【问题标题】:invalid_token when trying to access Facebook through ACAccount and SLRequest尝试通过 ACAccount 和 SLRequest 访问 Facebook 时的 invalid_token
【发布时间】:2014-02-28 03:52:24
【问题描述】:

我正在尝试从用户的 Facebook 收件箱中读取消息,但我遇到了拒绝我访问的错误。这是我的代码:

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;
} }

我不确定为什么会出现这个问题。我在这里做错了什么?

【问题讨论】:

    标签: ios objective-c facebook acaccount slrequest


    【解决方案1】:

    设备上的 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 选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多