我正在使用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;
}
}];
}
答案 0 :(得分:1)
如果Facebook SDK在自己执行Facebook图形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)
}];
}