36

iOS beginner here. I have the following code:

[facebook authorize:nil delegate:self];
NSString *string1=[facebook accessToken];
NSLog(string1);

The log shows: miFOG1WS_7DL88g6d95Uxmzz7GCShsWx_FHuvZkmW0E.eyJpdiI6IjNZZkFBY1c5ZnBaMGEzOWM2RzNKbEEifQ.LNjl06lsOQCO9ArVARqff3Ur2XQHku3CMHDBKkpGg351EB33LGxVv96Hh5R860KDJL0cLq8QezSW0GovYxnhUucOwxlITV364sVKDXIzC3bAn9P_74r2Axl1SgOaTyMMkQ_aSQ2OWh-8d3Zn9BDt3pXVWzBLJ9I4XAosnw0GjuE

This seems too long to be an access token. I read it's supposed to be only 40 characters long. What am I doing wrong?

John
  • 5,835
  • 8
  • 28
  • 36

10 Answers10

121

Using v3.2.1 as of March 20, 2013 of Facebook SDK.

NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];

If you prefer dot syntax,

NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;

For those using Swift;

var fbAccessToken = FBSession.activeSession().accessTokenData.accessToken

Update for v4.1.0 SDK onwards

Objective-C

NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;

Swift

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
Nur Iman Izam
  • 1,613
  • 1
  • 15
  • 12
8

If you are using the Facebook SDK you can use the FBSession object to get and accessToken. If the user authenticated to the app and you have an active session use:

NSString * accessToken = [[FBSession activeSession] accessToken];

to get the access token.

It worked for me.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
abdus.me
  • 1,819
  • 22
  • 34
  • 2
    The method suggested by abdus.me is deprecated in the 3.2 SDK. The proper way to access the accessToken is the one suggested by Nur Iman Izam: if ([[FBSession activeSession] isOpen]) { NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken]; ... } – Rool Paap Apr 02 '13 at 14:22
4

For SDK 4.0

NSString *fbAccessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
enagra
  • 2,296
  • 1
  • 18
  • 19
4

For Swift 1.2 and FB SDK 4.1:

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
Andrew K
  • 1,571
  • 1
  • 17
  • 25
4

You can use delegate method:

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate 

for more information you can refer to the following question :Facebook Access Token

Community
  • 1
  • 1
Sarah
  • 1,595
  • 3
  • 25
  • 34
  • 1
    I'm not sure about the length there is no fixed one as you can see on that question http://stackoverflow.com/questions/4408945/what-is-the-length-of-the-access-token-in-facebook-oauth2 – Sarah May 16 '11 at 11:27
3

For FacebookLogin (0.9.0) Swift

import FacebookLogin

AccessToken.current?.tokenString
kuadbam
  • 31
  • 1
2
- (IBAction)facebookClick:(id)sender {

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

     [login logOut];

    login = [[FBSDKLoginManager alloc] init];


    login.loginBehavior = FBSDKLoginBehaviorWeb;
    [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error)
        {
                 NSLog(@"Process error");
             //   [login logOut];
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
            // [login logOut];
         } else {
             NSLog(@"Logged in");
             [self getUserInformation];
         }
     }];
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Whilst this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question not only for this person, but for readers in the future, who might not know why your code is the way it is. – Claudia Jun 18 '16 at 22:05
1

Check the code below for getting the access_token from ACAccountStore :

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType =  [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *FBOptions = [NSDictionary dictionaryWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];

[accountStore requestAccessToAccountsWithType:accountType options:FBOptions completion:
 ^(BOOL granted, NSError *error) {
     if (granted) {

         NSArray *facebookAccounts = [accountStore accountsWithAccountType:accountType];
         FBAccount = [facebookAccounts firstObject];
         NSLog(@"token :%@",[[FBAccount credential] oauthToken]);

     } else {
         //Error
         NSLog(@"error getting permission %@",error);
         if([error code]== ACErrorAccountNotFound){
             NSLog(@"Account not found. Please setup your account in settings app"); 
         }
         else {
             NSLog(@"Account access denied");
         }

     }
 }];
KarimIhab
  • 173
  • 1
  • 8
1

Swift 4 (2018)

FBSDKAccessToken.current().tokenString
Awais Fayyaz
  • 2,275
  • 1
  • 22
  • 45
0

For people who need to use swift with SDK 3.2.1:

let token = FBSession.activeSession().accessTokenData.accessToken
Brian
  • 30,156
  • 15
  • 86
  • 87