Generating FB User Access Token

Posted By : Avilash Choudhary | 28-Dec-2015

Different platforms have different methods to kick off this process and include functionality to manage access token on behalf of developer and person granting permissions.

Javascript
Facebook SDK for Javascript obtains and go through tokens automatically in browser cookies. Retrieve by making a call to FB.getAuthResponse which will include accesstoken property within response.

Android
Facebook SDKs for android automatically manages this through class com.facebook.AccessToken. Retrieve by inspecting Session.getCurrentAccessToken

iOS
Facebook SDKs for iOS automatically manages this through class FBSDKAccessToken. Retrieve by inspecting FBSDKAcessToken.currentAccessToken

Web (without Javascript)

When building application with on web without Facebook’s SDK for javascript you will need generate access token during the steps

JavaScript SDK

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    var accessToken = response.authResponse.accessToken;
  } 
} );

 

Android SDK

 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this.getApplicationContext());
    accessToken = AccessToken.getCurrentAccessToken();
}

iOS SDK

- (void)viewDidLoad
{
  [super viewDidLoad];
  NSString *accessToken = [FBSDKAccessToken currentAccessToken];
}

 

Short Term and Long term token
Long lived tokens have lifetime about 60 days while Short lived tokens have lifetime about an hour or two.You should not depend on these lifetime remaining same -  lifetime may change without waring or expire early.
Mobile applications that use Facebook’s Android and iOS SDKs get long lived tokens by default. Generated via web login are short lived tokens, but you can convert them to long-lived tokens by making server side API call along with your application secret.

THANKS

About Author

Author Image
Avilash Choudhary

Avilash has excellent experience in developing mobile and web applications using jQuery , Javascript and PhoneGap. His hobbies are watching and playing cricket.

Request for Proposal

Name is required

Comment is required

Sending message..