2014-02-18 1 views
0

사용자의 Facebook 북의받은 편지함에서 메시지를 읽으려고하는데 액세스를 거부하는 오류가 발생합니다.ACAccount 및 SLRequest를 통해 Facebook에 액세스하려고 할 때 invalid_token

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
    ACAccountType *fbAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 
    NSDictionary *options = @{ 
           ACFacebookAppIdKey : FacebookAppKey, 
           ACFacebookPermissionsKey : FacebookPermissionsArray 
           }; 
    [accountStore requestAccessToAccountsWithType:fbAccountType options:options completion:^(BOOL granted, NSError *error) 
    { 
     if (error || !granted) { 
      return; 
     } 
     NSArray *accounts = [accountStore accountsWithAccountType:fbAccountType]; 

     if (accounts.count == 0) { 
      return; 
     } 

     ACAccount *facebookAccount = [accounts lastObject]; 

     NSURL *inboxURL = [NSURL URLWithString:@"https://graph.facebook.com/me/inbox"]; 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 

     SLRequest *facebookInfoRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook 
                  requestMethod:SLRequestMethodGET 
                     URL:inboxURL 
                   parameters:parameters]; 
     [facebookInfoRequest setAccount:facebookAccount]; 
     [facebookInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 

      NSLog(@"response data: %i, url response: %@, error: %@", responseData.length, urlResponse, error); 
     }]; 
    }]; 

이에 대한 URL 응답이 읽습니다 :

<NSHTTPURLResponse: 0xcb5fa10> { URL: https://graph.facebook.com/me/inbox?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX } { status code: 400, headers { 
    "Access-Control-Allow-Origin" = "*"; 
    "Cache-Control" = "no-store"; 
    Connection = "keep-alive"; 
    "Content-Length" = 194; 
    "Content-Type" = "application/json; charset=UTF-8"; 
    Date = "Tue, 18 Feb 2014 15:29:36 GMT"; 
    Expires = "Sat, 01 Jan 2000 00:00:00 GMT"; 
    Pragma = "no-cache"; 
    "Www-Authenticate" = "OAuth \"Facebook Platform\" \"invalid_token\" \"Error validating access token: Session has expired on Feb 13, 2014 6:17am. The current time is Feb 18, 2014 7:29am.\""; 
    "X-FB-Debug" = "XXXXXXXXXXXXXXXX/Y+K0w="; 
    "X-FB-Rev" = XXXXXXXX; 
} } 

이 문제가 일어난 이유는 확실 해요 이건 내 코드입니다. 내가 여기서 뭘 잘못 할 수 있니?

답변

3

기기의 Facebook 계정이 서버 및 SDK의 캐시와 동기화되지 않습니다. 업데이트를 강제로 수행하려면 ACAccountStore's 방법 renewCredentialsForAccount으로 전화하십시오.

- (void)refreshFacebookTokenStatus 
{ 
    ACAccountStore *accountStore; 
    ACAccountType *accountTypeFB; 

    if ((accountStore = [[ACAccountStore alloc] init]) && 
     (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook])) 
    { 
     NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB]; 

     id account; 

     if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])) 
     { 
      [accountStore renewCredentialsForAccount:account 
              completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) 
      { 
       if (error) 
       { 
        NSLog(@"%@", error.localizedDescription); 
       } 
      }]; 
     } 
    } 
} 

가장 간단한 방법은 앱을 시작할 때마다 호출하는 것이지만 이는 효율적이지 않을 수 있습니다. 따라서 세션 상태가 변경 될 때 호출하는 것이 좋습니다.

앱 개발자가 아닌 Facebook 사용자와 로그인하려는 경우 Facebook 앱 설정에 Sandbox 옵션이 설정되어 있지 않은지 확인하십시오.

관련 문제