在 Facebook iOS 3.1.1 SDK 中使用 FBSession openActiveSessionWithReadPermissions 处理无效的 accessToken [英] Handle invalid accessToken with FBSession openActiveSessionWithReadPermissions in Facebook iOS 3.1.1 SDK

查看:33
本文介绍了在 Facebook iOS 3.1.1 SDK 中使用 FBSession openActiveSessionWithReadPermissions 处理无效的 accessToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前,我已经阅读了这个this 问题解决下面和问之前的问题.

Before anything, I have read both this and this questions to solve the problem below and before asking.

我的问题是,当 accessToken 过期时(因为过期日期已过,或者通过从我的 Facebook 的应用中心手动删除应用程序)以下代码:

My problem is that when the accessToken gets expired (either because the expiration date passes, or manually by deleting the app from my Facebook's App Center) the following code:

if ([[FBSession activeSession] isOpen]) {
        //do something
    }
else {
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            if(FB_ISSESSIONOPENWITHSTATE(status)) {
                //do something
            }
          }
       }];
     }

在 FBSession.activeSession 打开的情况下进入 else 块,但是当执行做某事"时,accessToken 无效,因此请求会收到错误:HTTP 状态代码:400.当我尝试立即执行两次整个过程时,FBSession 请求许可(iOS6 集成 Facebook、Facebook 应用程序或 Safari 中的 Facebook 网站的 UIAlertView),其余运行顺利.

gets in the else block with FBSession.activeSession open but when the 'do something' is executed the accessToken is invalid so the request gets Error: HTTP status code: 400. When I try to do the whole procedure twice immediately the FBSession asks for permission (either UIAlertView for iOS6 integrated facebook, Facebook App or Facebook website in Safari) and the rest runs smoothly.

我担心的是为什么我必须做两次才能正常工作,以及为什么 Facebook SDK 无法在第一次检测到 activeSession 和 accessToken 无效.

My concern is why I have to do everything twice to work well and why Facebook SDK cannot detect in the first time that the activeSession and accessToken are invalid.

先谢谢大家!

推荐答案

您链接的问题是相关的,尤其是 Facebook SDK 3.1 - 验证访问令牌时出错 这解释了设备上的 Facebook 帐户与服务器不同步的问题(即,如果您从 App Center 中删除了应用程序).正如那里提到的,在 3.1.1 中,只有当 SDK 从服务器收到无效响应时,它才会调用更新设备令牌.这是为了减少到服务器的往返次数而带来的便利性权衡.

The questions you linked are relevant, especially Facebook SDK 3.1 - Error validating access token which explains the problem where the Facebook account on the device is out of sync with the server (I.e., if you deleted the app from App Center). As mentioned there, in 3.1.1 the SDK will call to renew the device token only when it gets the invalid response from the server. This is a trade off in convenience for less round-trips to the server.

假设您的代码块在 applicationDidFinishLaunching 或类似的东西上执行,它将转到 else 块,因为应用程序从一个新会话开始.当它调用 openActiveSessionWithReadPermissions 时,iOS 6 设备认为令牌有效,并将让状态进入 Open,然后你的做某事"被执行.只有这样,SDK 才会从服务器获取无效响应并使设备令牌无效.因此,下次调用该过程时,它会适当地提示用户再次授权.

Assuming your code block is executed on applicationDidFinishLaunching or something similar, it will go to the else block because the app starts with a new session. When it calls openActiveSessionWithReadPermissions, the iOS 6 device thinks the token is valid and will let the state go to Open, so then your "do something" gets executed. Only then does the SDK get the invalid response from the server and invalidate the device token. As a result, the next time the procedure is called, it will prompt the user appropriately to authorize again.

这是故意的.现在,如果错误代码描述了无效令牌,您可以考虑在应用程序中自动重试.例如,请参阅 Scrumptious 示例 postOpenGraph 重试代码.在您的情况下,它可能看起来更接近于(出于演示目的,我使用 requestForMe 作为做某事"):

This is intentional. For now, you can consider a automatic retry in your application if the error code describes an invalid token. For example, see the Scrumptious sample postOpenGraph retry code. In your case, it may look closer to something like (I used requestForMe as the "do something" for demonstration purposes):

else {
    [FBSessionopenActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        if(FB_ISSESSIONOPENWITHSTATE(status)) {
            //do something
            [[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error){
                    NSLog(@"success on first try");
                } else if ([[error userInfo][FBErrorParsedJSONResponseKey][@"body"][@"error"][@"code"] compare:@190] == NSOrderedSame) {
                    //requestForMe failed due to error validating access token (code 190), so retry login
                    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                        if (!error){
                            //do something again, or consider recursive call with a max retry count.
                            NSLog(@"success on retry");
                        }
                    }];
                }
            }];
        }
    }];
}

这篇关于在 Facebook iOS 3.1.1 SDK 中使用 FBSession openActiveSessionWithReadPermissions 处理无效的 accessToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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