简体   繁体   English

在Facebook iOS 3.1.1 SDK中使用FBSession openActiveSessionWithReadPermissions处理无效的accessToken

[英]Handle invalid accessToken with FBSession openActiveSessionWithReadPermissions in Facebook iOS 3.1.1 SDK

Before anything, I have read both this and this questions to solve the problem below and before asking. 在开始之前,我已经阅读了这个问题和这个问题,以解决下面和提出之前的问题。

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: 我的问题是,当accessToken过期时(因为过期日期已过,或者是通过从我的Facebook应用程序中心中删除该应用程序来手动进行),以下代码:

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

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. 在打开FBSession.activeSession的情况下进入else块,但是当执行“执行某些操作”时,accessToken无效,因此请求得到错误:HTTP状态代码:400。当我尝试立即执行两次整个过程时,FBSession寻求权限(适用于iOS6的UIAlertView集成了Facebook,Facebook App或Safari中的Facebook网站),其余的运行顺利。

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在第一次无法检测到activeSession和accessToken无效。

Thank you all in advance! 谢谢大家!

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 (Ie, if you deleted the app from App Center). 您链接的问题是相关的,尤其是Facebook SDK 3.1-验证访问令牌时出错 ,这说明了设备上的Facebook帐户与服务器不同步的问题(即,如果您从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. 如此处所述,在3.1.1中,SDK仅在收到来自服务器的无效响应时才调用更新设备令牌。 This is a trade off in convenience for less round-trips to the server. 这是为了减少往返服务器的便利性而进行的折衷。

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. 假设您的代码块是在applicationDidFinishLaunching或类似的代码上执行的,那么它将进入else块,因为该应用程序从一个新的会话开始。 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. 当它调用openActiveSessionWithReadPermissions时,iOS 6设备认为令牌是有效的,并将让状态进入“打开”状态,因此您的“执行操作”将被执行。 Only then does the SDK get the invalid response from the server and invalidate the device token. 只有这样,SDK才会从服务器获取无效响应,并使设备令牌无效。 As a result, the next time the procedure is called, it will prompt the user appropriately to authorize again. 结果,下次调用该过程时,它将提示用户适当地再次授权。

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. 例如,请参见Scrumptious示例postOpenGraph重试代码。 In your case, it may look closer to something like (I used requestForMe as the "do something" for demonstration purposes): 在您的情况下,它看起来更像以下内容(出于演示目的,我将requestForMe用作“执行某项操作”):

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");
                        }
                    }];
                }
            }];
        }
    }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM