3

我正在使用 Facebook iOS SDK(3.2 版),一切正常,直到用户进入 Facebook 并从授权应用程序列表中删除该应用程序。那时,我仍然使用缓存的会话登录,即使它实际上不再有效。强制清除访问令牌并再次登录也会导致使用相同的旧访问令牌。

现在application:didFinishLaunchingWithOptions:我有这段代码来测试会话:

// Relogin with cached session
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    NSLog(@"We're logged in to Facebook!");

    [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:NO completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        NSLog(@"accessToken: %@", session.accessTokenData.accessToken);
        NSLog(@"status: %i", status);
        NSLog(@"error: %@", error);

        switch (status) {
            case FBSessionStateOpen:
                NSLog(@"FBSessionStateOpen");
                NSLog(@"permissions: %@", session.permissions);
                break;

            case FBSessionStateClosed:
            case FBSessionStateClosedLoginFailed:
                [session closeAndClearTokenInformation];
                [Utilities presentMessage:@"Facebook login failed" withTitle:@"Error" level:MessageLevelError];
                break;

            default:
                break;
        }
    }];
}

每次我启动应用程序时,它都会说我仍然使用 user_birthday 和电子邮件权限登录。但是,当我尝试使用我获得的 accessToken 打开https://graph.facebook.com/me?access_token=xxx时,它返回“验证访问令牌时出错:用户 123 尚未授权应用程序 123。”

清除令牌[FBSession.activeSession closeAndClearTokenInformation];并强制新登录无济于事,我总是得到完全相同的旧 accessToken。

有一个类似的问题(Facebook SDK 3.1 iOS: Handle login if user remove app from Facebook Settings),但它指出“然后,用户关闭 iOS 应用程序并重新启动它......通过这次重新启动,iOS 应用程序”是已修复”并检测到用户不再登录。” 我完全没有这种行为。

这就是我首先打开会话的方式:

- (void)openFBSession {
    NSArray *perm = @[ @"user_birthday", @"email" ];

    [FBSession openActiveSessionWithReadPermissions:perm allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        DLog(@"accessToken: %@", session.accessTokenData.accessToken);
        DLog(@"status: %i", status);
        DLog(@"error: %@", error);

        switch (status) {
            case FBSessionStateOpen:
                DLog(@"FBSessionStateOpen");
                // this is where I need to use session.accessTokenData.accessToken, which is stale
                break;

            case FBSessionStateClosed:
            case FBSessionStateClosedLoginFailed:
                [FBSession.activeSession closeAndClearTokenInformation];
                [Utilities presentMessage:@"Facebook login failed" withTitle:@"Error" level:MessageLevelError];
                break;

            default:
                break;
        }
    }];
}
4

1 回答 1

1

如果 Facebook SDK 在自己执行 Facebook graph api 请求时没有发现令牌无效,则需要强制 iOS 检查其令牌。

当您调用自己的服务器 api(并给它令牌)并代表那里的用户进行调用时,这很有用。如果您发现令牌在那里无效,那么告诉 iOS 6 令牌无效的唯一方法是执行以下操作:

// Only do this if there is native Facebook Account support (iOS 6+)
BOOL isFacebookAccountsAvailable = (&ACAccountTypeIdentifierFacebook != NULL);
if (isFacebookAccountsAvailable) {
    ACAccountStore *accountStore = [ACAccountStore new];
    ACAccountType *accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];

    for (ACAccount *fbAccount in fbAccounts)
        [accountStore renewCredentialsForAccount:fbAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
            DLog(@"Renew account error: %@", error.localizedDescription)
        }];
}
于 2013-03-27T22:26:56.490 回答