2014-02-18 93 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選項。