Jack Sitt
Jack Sitt

Reputation: 21

FBSDKAccessToken currentAccessToken is nil

Code:

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

I log in with the code above. For some reason NSString *accessToken = [facebookCredential oauthToken] stores the access token correctly but FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken] does not.

Is there any way to covert the NSString *accessToken to type FBSDKAccessToken?

Upvotes: 2

Views: 1815

Answers (3)

Goyani Maulik
Goyani Maulik

Reputation: 38

In My case I forgot to add below line in didFinishLaunchingWithOptions

[[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];

Other Reason is if you call [FBSDKAccessToken currentAccessToken] before didFinishLaunchingWithOptions that it will alway return nil

I hope it will work for you...

Upvotes: 0

ChrisH
ChrisH

Reputation: 4558

I ran into a similar problem, and it was due to me not setting enableUpdatesOnAccessTokenChange on FBSDKProfile. Not sure why it doesn't do this by default.

[FBSDKProfile enableUpdatesOnAccessTokenChange:TRUE];

Or for Swift

FBSDKProfile.enableUpdatesOnAccessTokenChange(true)

Upvotes: 2

meth
meth

Reputation: 1885

You can reach the accessToken string representation by calling tokenString parameter in FBSDKAccessToken.

 FBSDKAccessToken* AccessToken = [FBSDKAccessToken currentAccessToken];

 NSString *tokenString = AccessToken.tokenString; 

to explicitly create accessToken, there is only one method in a document and it is:

- (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;

however there is also discussion part for this and states that:

This initializer should only be used for advanced apps that manage tokens explicitly. Typical login flows only need to use FBSDKLoginManager along with +currentAccessToken.

Upvotes: -1

Related Questions