Facebook SDK 3.1 - 验证访问令牌时出错 [英] Facebook SDK 3.1 - Error validating access token

查看:31
本文介绍了Facebook SDK 3.1 - 验证访问令牌时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的应用转换到新的 Facebook SDK 3.1(支持 iOS6 身份验证).

I'm trying to transition my app to the new Facebook SDK 3.1 (with support for iOS6 authentication).

我让它运行得很好,所以我决定从我在 FB 网站上的授权应用列表中删除该应用,以测试 iOS 是否会再次请求许可.

I had it working just fine, so I then decided to remove the app from my list of authorized apps on the FB website in order to test that iOS would ask for permission again.

现在我第一次调用 [FBRequest requestForMe] 导致这个错误:

Now my first call to [FBRequest requestForMe] causes this error:

回复:

{
  "error": {
    "message": "Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.",
    "type":"OAuthException",
    "code":190,
    "error_subcode":460
  }
}

一些细节:

我正在尝试按如下方式打开会话:

I'm trying to open the session as follows :

   [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

                                           switch (state) {
                                               case FBSessionStateOpen:
                                                   [self presentPostOptions];
                                                   break;

                                               case FBSessionStateClosed:
                                               case FBSessionStateClosedLoginFailed:
                                                   [FBSession.activeSession closeAndClearTokenInformation];
                                                   break;

                                               default:
                                                   break;
                                           }

然后我在 FBSessionStateOpen 状态被回调(此时 iOS 还没有显示请求对话框,这是意料之中的)?Facebook 记录了这个:

I then get called back in state FBSessionStateOpen (at this point iOS hasn't presented a request dialog, is that to be expected)? Facebook logs this:

2012-09-26 13:43:43.768 MyApp[2177:907] FBSDKLog: FBSession INVALID transition from FBSessionStateCreated to FBSessionStateClosed
2012-09-26 13:43:43.769 MyApp[2177:907] FBSDKLog: FBSession transition from FBSessionStateCreated to FBSessionStateCreatedOpening 
2012-09-26 13:43:43.837 MyApp[2177:907] FBSDKLog: FBSession transition from FBSessionStateCreatedOpening to FBSessionStateOpen 

会话打开后,在presentPostOptions中我这样做:

Once the session is open, in presentPostOptions I do this:

- (void)presentPostOptions
{    
    [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
        if (!error) {
            self.usersName = user.name;
            self.usersID = user.id;

            [self getPages];
        }
        else
        {
            [self didFailWithError:error];
        }
    }];
}

在回调上述完成块之前,我的主状态处理程序块以 FBSessionStateClosed 状态调用.与此同时,Facebook SDK 记录了上述错误.

Before the above completion block is called back, my main state handler block is called with an FBSessionStateClosed state. In the meantime, the Facebook SDK has logged the above error.

我找不到任何重置系统的方法;我也不太明白原因.

I can't find any way to reset the system; nor do I really understand the cause.

有人可以透露一些信息吗?

Can anyone please shed some light?

推荐答案

设备上的 Facebook 帐户与服务器以及应用程序/SDK 的缓存不同步.这可以通过调用 ACAccountStore 方法renewCredentialsForAccount 来解决,这将更新操作系统对令牌状态的理解.

The Facebook account on the device has become out-of-sync with the server as well as with the App's/SDK's cache. This can be solved by calling the ACAccountStore method renewCredentialsForAccount, which will update the OS's understanding of the token state.

SDK 下次更新时,SDK 会在收到服务器响应令牌失效时自动调用该 API.对于 SDK 的 3.1.0 修订版,应用程序将需要显式调用此 API.这是一个代码示例:

In the next update of the SDK, the SDK will automatically call this API when it receives a response from the server indicating that a token has become invalid. For the 3.1.0 revision of the SDK, applications will need to explicitly call this API. Here is a code sample:

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) {
            //we don't actually need to inspect renewResult or error.
            if (error){

            }
        }];
    }
}

在何处/何时调用 API 有多种选择.最简单的地方是在应用程序启动或视图加载时机会性地进行调用.这种方法的一个问题是它会导致通常不必要的网络往返.另一种选择是在发生会话更改通知时调用它,表明会话已关闭.此外,许多应用程序会在应用程序启动时获取一些基本信息,例如 graph.facebook.com/me,如果是这样 - 在错误响应的情况下调用此方法可能是要求 iOS 更新其令牌的合理位置地位.

There are several options for where/when to call the API. The simplest place would be to opportunistically make the call on application launch, or on view load. One problem with this approach is that it will cause a network round-trip that is often unnecessary. Another option is to call it when a session change notification occurs, indicating that a session has closed. Also many applications fetch some basic information such as graph.facebook.com/me, at application launch time, and if so -- a call to this method in case of an error response may be a reasonable place to ask iOS to update its token status.

希望这会有所帮助!

这篇关于Facebook SDK 3.1 - 验证访问令牌时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆