Facebook Developers
DocsToolsSupportNewsApps
Log In
  • Social Plugins
  • Facebook Login
  • Open Graph
  • Facebook APIs
  • Games
  • Media
  • Payments
  • App Center
  • Promote Your App
  • iOS
  • Android
  • Web
  • Technology Partners
  • Methods
    • API Requests
    • Async API Requests
    • Authentication
    • Dialogs
    • Helpers
  • Interfaces
    • -
    • DialogListener
  • Errors
    • DialogError
    • FacebookError

Authentication

The authentication methods used in the Facebook Android SDK. These methods are defined in Facebook.java.

You implement these methods to do the following:

  • Initialize the Facebook instance with your app ID. Once initialized the instance can be used for follow-on method calls.

  • Initiate the user authentication and app authorization flow. Once this is completed successfully you can make additional API calls that require the access token.

  • Handle the Single Sign-On URL (SSO) callback. This is part of the SSO implementation.

  • Log out the user from Facebook.

  • Check if the user session is valid. You may do this to check if an access_token is valid so you can handle re-authenticating the user. You may also do this to expose functionality that requires the user to be logged in.

Instance Method:


Facebook(String appId):

Constructor for the Facebook object.

  • Creates a Facebook object.
  • Returns IllegalArgumentException if no appID is provided.

Parameters

appId - The Facebook application id.

Return Value

None

Notes

This method must be called before making any further calls to the Facebook API.

Authentication Methods:


void authorize(Activity activity, final DialogListener listener):

Starts either an Activity or a dialog which prompts the user to log in to Facebook and grant the requested permissions to the given application.

  • Authorizes the app
  • Only basic permissions are granted. Refer to Permissions for more info on permissions.
  • This method is asynchronous and the callback will be invoked in the original calling thread (not in a background thread).
  • Single Sign On is triggered via activity.startActivityForResult(). As such, you will need to implement onActivityResult() method in your calling activity and include a call to the authorizeCallback() for it to work properly.

Parameters

activity - Android activity in which we want to display the authorization dialog

listener - Callback com.facebook.android.Facebook.DialogListener interface for notifying the calling application when the authentication dialog has completed, failed, or been canceled.

Return Value

None. On completion of authentication flow, appropriate method in the DialogListener interface is called.

Notes

This method will trigger Single Sign On if the Facebook app is installed on the handset, else will display the Login Dialog in an embedded webview.


void authorize(Activity activity, String[] permissions, final DialogListener listener):

Another version of the authorize method with an additional parameter - permissions.

Additional parameters

permissions - A list of permissions required for this application: e.g. "read_stream", "publish_stream", "offline_access", etc. see Permissions. This parameter should not be null -- if you do not require any permissions, then pass in an empty String array.

Refer above for more details.


void authorize(Activity activity, String[] permissions, int activityCode, final DialogListener listener):

Another version of the authorize method with an additional parameter - activityCode.

Additional parameters

activityCode - Single sign-on requires an activity result to be called back to the client application. If you are waiting on other activities to return data, pass a custom activity code here to avoid collisions. If you would like to force the use of legacy dialog-based authorization, pass FORCE_DIALOG_AUTH for this parameter. If this parameter is not specified, Facebook will use a suitable default. See Android Activity for more information.


void authorizeCallback(int requestCode, int resultCode, Intent data):

This method must be invoked at the top of the calling activity's onActivityResult() function for SSO to work properly.

Parameters

Single Sign On when finished will call the OnActivityResult(int requestCode, int resultCode, Intent data) in the calling activity. You can use the requestCode to handle collision if needed, and pass back those parameters in call to this method.

Return Value

One of the onComplete(), onFacebookError(), onCancel(), or onError() in the DialogListener class will be called at the end of authentication flow.

Logout Methods:


String logout(Context context) throws MalformedURLException, IOException :

Invalidate the current user session by removing the access token in memory, clearing the browser cookie, and calling (auth.expireSession](/docs/reference/rest/auth.expireSession/) through the API.

Parameters

context - The Android context in which the logout should be called, it should be the same context in which the login occurred in order to clear any stored cookies

Exceptions

MalformedURLException - if accessing an invalid endpoint

IOException - if a network error occurs

Return Value

JSON string representation of the auth.expireSession response("true" if successful).

Notes

Note that this method blocks waiting for a network response, so do not call it in a UI thread.

Updated over a year ago
Facebook © 2013 · English (US)
AboutAdvertisingCareersPlatform PoliciesPrivacy Policy