FBSDKAccessToken currentAccessToken为零

时间:2016-02-03 07:58:55

标签: ios objective-c

代码:

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]正确存储了访问令牌,但FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken]没有。

有没有办法将NSString *accessToken转换为FBSDKAccessToken

3 个答案:

答案 0 :(得分:2)

我遇到了类似的问题,原因是我没有在public static <AnyType extends Comparable<AnyType>> int binarySearch (AnyType[] a, AnyType x) { int nMin = 0; int nMax = a.length - 1; int nMid = (nMin + nMax) / 2; int compare = a[nMid].compareTo(x); if(compare == 0) return 1; else if(compare == 1) return binarySearch(Arrays.copyOfRange(a, 0, a.length/2), x); else return binarySearch(Arrays.copyOfRange(a, a.length/2, nMax), x); } 上设置enableUpdatesOnAccessTokenChange。不知道为什么默认情况下它没有这样做。

FBSDKProfile

或者对于Swift

[FBSDKProfile enableUpdatesOnAccessTokenChange:TRUE];

答案 1 :(得分:0)

您可以通过调用FBSDKAccessToken中的tokenString参数来访​​问accessToken字符串表示。

 FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken];

 NSString *tokenString = AccessToken.tokenString; 

要显式创建accessToken,document中只有一个方法,它是:

- (instancetype)
initWithTokenString:    (NSString *)tokenString
permissions:    (NSArray *)permissions
declinedPermissions:    (NSArray *)declinedPermissions
appID:  (NSString *)appID
userID: (NSString *)userID
expirationDate: (NSDate *)expirationDate
refreshDate:    (NSDate *)refreshDate
NS_DESIGNATED_INITIALIZER;

但是也有讨论的部分,并指出:

此初始化程序仅应用于明确管理令牌的高级应用程序。典型的登录流程只需使用FBSDKLoginManager+currentAccessToken

答案 2 :(得分:0)

就我而言,我忘记在 didFinishLaunchingWithOptions

中添加以下行
[[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];

其他原因是,如果您在 didFinishLaunchingWithOptions 之前调用 [FBSDKAccessToken currentAccessToken] ,它将始终返回 nil

我希望它对您有用...