OStack程序员社区-中国程序员成长平台

标题: ios - FBSDKAccessToken currentAccessToken 为 nil [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:25
标题: ios - FBSDKAccessToken currentAccessToken 为 nil

代码:

self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore
                               accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSString *appID = @"356775877779749"; //The app ID issued by Facebook
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:
                        appID, ACFacebookAppIdKey,
                        @[@"email"], ACFacebookPermissionsKey,
                        nil];

[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
 ^(BOOL granted, NSError *e) {
     if (granted)
     {
         NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
         //it will always be the last object with single sign on
         self.facebookAccount = [accounts lastObject];


         ACAccountCredential *facebookCredential = [self.facebookAccount credential];
         NSString *accessToken = [facebookCredential oauthToken];
         NSLog(@"Successfully logged in. Access Token: %@", accessToken);


         FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken];
         NSLog(@"%@", AccessToken);
         [self newFacebookLogin];
     } else {
         //Failed
         NSLog(@"error getting permission: %@",e);
     }
 }];

我使用上面的代码登录。出于某种原因,NSString *accessToken = [facebookCredential oauthToken] 正确存储了访问 token ,但 FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken] 没有。

有什么方法可以将 NSString *accessToken 转换为 FBSDKAccessToken



Best Answer-推荐答案


我遇到了类似的问题,这是由于我没有在 FBSDKProfile 上设置 enableUpdatesOnAccessTokenChange。不知道为什么它默认不这样做。

[FBSDKProfile enableUpdatesOnAccessTokenChange:TRUE];

或者对于 swift

FBSDKProfile.enableUpdatesOnAccessTokenChange(true)

关于ios - FBSDKAccessToken currentAccessToken 为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171416/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4