ios Facebook SDK 3.1 - 验证访问令牌时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12601191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:23:54  来源:igfitidea点击:

Facebook SDK 3.1 - Error validating access token

iosfacebookios6facebook-ios-sdkfacebook-sdk-3.1

提问by tarmes

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

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

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.

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

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

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

Response:

回复:

{
  "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
  }
}

Some details:

一些细节:

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

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:

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

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 

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

一旦会话打开,在 presentPostOptions 我这样做:

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

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

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

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

我找不到任何重置系统的方法;我也不真正了解原因。

Can anyone please shed some light?

任何人都可以请说明一下吗?

回答by Jason Clark

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.

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

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:

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

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){

            }
        }];
    }
}

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.

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

Hopefully this helps!

希望这会有所帮助!

回答by snowdragon

I'm just going to contribute another thing to check that caused me to waste 3 hours: Make sure your FB app settings do not have the 'Sandbox' option on if you're trying to login with a non-app-developer FB user... Maybe obvious, but could save others a few hours hopefully.

我只是要贡献另一件事来检查这让我浪费了 3 个小时:如果您尝试使用非应用程序开发人员 FB 用户登录,请确保您的 FB 应用程序设置没有打开“沙盒”选项...也许很明显,但希望可以为其他人节省几个小时。

回答by Cyupa

Try adding, if you haven't already, your iOS App Bundle ID in the settings panelof your Facebook APP as suggested here.

按照此处的建议,尝试在Facebook APP的设置面板中添加您的 iOS App Bundle ID(如果您尚未添加)。

Hope this helps.

希望这可以帮助。

回答by Min Kim

I'm fairly sure this is a Facebook iOS SDK bug (even on 3.1.1) and I filed this bug report.

我相当确定这是 Facebook iOS SDK 错误(即使在 3.1.1 上),并且我提交了此错误报告

While trying to reproduce this bug using their sample app Scrumptious, I found that it allows you to successfully re-authorize if you are using openActiveSessionWithReadPermissions. However, if you are asking for publish permissions via openActiveSessionWithPublishPermissions, it would be stuck in com.facebook.sdk.error 5.

在尝试使用他们的示例应用程序 Scrumptious 重现此错误时,我发现如果您使用openActiveSessionWithReadPermissions. 但是,如果您通过 请求发布权限openActiveSessionWithPublishPermissions,它将被困在com.facebook.sdk.error 5.

回答by Min Kim

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

      }];   

回答by fatihyildizhan

Simple Swift 2+ Solutionfor Facebook Error Validating Access Token

用于 Facebook 验证访问令牌错误的简单 Swift 2+ 解决方案

// Step 1: Logout 
FBSDKLoginManager().logOut()

// Step 2: Login
FBSDKLoginManager().logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: { result, error -> Void in 

// ...

}

If this error occurs, you have to create new Facebook Session, so you have to login again.

如果发生此错误,您必须创建新的Facebook Session,因此您必须再次登录。

回答by Tiago Almeida

In facebook SDK 3.7.1 I still had this issue. Basically I have decided to clear the token from the facebook cache when this happens. Like this:

在 facebook SDK 3.7.1 中,我仍然有这个问题。基本上我决定在发生这种情况时从 facebook 缓存中清除令牌。像这样:

if(state == FBSessionStateClosed ) {
     [FBSession.activeSession closeAndClearTokenInformation];
}