Account Kit for Android - Quickstart

See Next Steps for optional customizations you can perform.

For an example project that illustrates how to integrate Account Kit, see Account Kit Samples for Android on GitHub.

For common issues encountered during integration, see the FAQ section.

1. Login

Please log in to Facebook to create apps or register as a developer.

2. Choose Your App Settings

Choose whether to allow email and SMS login, and choose security settings for your app. For more information on choosing your acesss token setting, see Access Tokens, and for information on choosing your app secret setting, see Using the Graph API.

3. Import the Account Kit SDK

Add the implementation dependency with the latest version of the Account Kit SDK in your app's build.gradle file:
repositories {
  jcenter()
}

dependencies {
  implementation 'com.facebook.android:account-kit-sdk:4.+'
}

4. Add XML Strings and Edit the Manifest

You need to login to complete this step.

5. Add Your Development and Release Key Hashes

You need to login to complete this step.

6. Check for Existing Sessions

If your app will receive the user's access token directly (i.e., the Enable Client Access Token Flow switch in your app's dashboard is ON) then you should check for a valid, existing token:
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.AccessToken;

AccessToken accessToken = AccountKit.getCurrentAccessToken();

if (accessToken != null) {
  //Handle Returning User
} else {
  //Handle new or logged out user
If your app will receive an authorization code that it will pass to the server (i.e. the Enable Client Access Token Flow switch in your app's dashboard is OFF), it is up to you to have your server communicate the correct login status to your client application.

7. Initiate a Login Flow for SMS

import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;

public static int APP_REQUEST_CODE = 99;

public void phoneLogin(final View view) {
  final Intent intent = new Intent(getActivity(), AccountKitActivity.class);
  AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
    new AccountKitConfiguration.AccountKitConfigurationBuilder(
      LoginType.PHONE,
      AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN
  // ... perform additional configuration ...
  intent.putExtra(
    AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
    configurationBuilder.build());
  startActivityForResult(intent, APP_REQUEST_CODE);
}
The APP_REQUEST_CODE is your custom code to track your login flow. It can be any integer, but it needs to be set by your application. When initializing your intent extras, be sure to specify the AccountKitActivity.ResponseType that matches your application's authorization setting in the Facebook developer portal dashboard: TOKEN if the Enable Client Access Token Flow switch in your app's dashboard is ON, and CODE if it is OFF. If people are logged into their Facebook account on their Android devices, and have a verified phone number, Account Kit verifies them without requiring them to enter the SMS code. For more information, see Instant Verification.

8. Initiate a Login Flow for Email

import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;

public static int APP_REQUEST_CODE = 99;

public void emailLogin(final View view) {
  final Intent intent = new Intent(getActivity(), AccountKitActivity.class);
  AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
    new AccountKitConfiguration.AccountKitConfigurationBuilder(
      LoginType.EMAIL,
      AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN
  // ... perform additional configuration ...
  intent.putExtra(
    AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
    configurationBuilder.build());
  startActivityForResult(intent, APP_REQUEST_CODE);
}
The APP_REQUEST_CODEis your custom code to track your login flow. It can be any integer, but it needs to be set by your application. When initializing your intent extras, be sure to specify the AccountKitActivity.ResponseType that matches your application's authorization setting in the Facebook developer portal dashboard: TOKEN if the Enable Client Access Token Flow switch in your app's dashboard is ON, and CODE if it is OFF. With Account Kit email login, people receive an email sent to their account. When they click on the link in the email on the same device that your app is installed on, they return to your app to finish the login activity. To return people to your app, add the AccountKitEmailRedirectActivity activity with the following intent filter to your AndroidManifest.xml file.
 <activity android:name="com.facebook.accountkit.ui.AccountKitEmailRedirectActivity">
   <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE" />
     <data android:scheme="@string/ak_login_protocol_scheme" />
  </intent-filter>
</activity>
And the following in your strings.xml file.
// if your Facebook App ID is 1234567, you should use ak1234567
<string name="ak_login_protocol_scheme">akFACEBOOK_APP_ID</string>

9. Handle the Activity's Result

Capture the Account Kit activity's result and extract the `AccountKitLoginResult` from the Intent argument to determine the status of the login attempt.
import com.facebook.accountkit.AccountKitLoginResult;

    @Override
    protected void onActivityResult(
            final int requestCode,
            final int resultCode,
            final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == APP_REQUEST_CODE) { // confirm that this response matches your request
            AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            String toastMessage;
            if (loginResult.getError() != null) {
                toastMessage = loginResult.getError().getErrorType().getMessage();
                showErrorActivity(loginResult.getError());
            } else if (loginResult.wasCancelled()) {
                toastMessage = "Login Cancelled";
            } else {
                if (loginResult.getAccessToken() != null) {
                    toastMessage = "Success:" + loginResult.getAccessToken().getAccountId();
                } else {
                    toastMessage = String.format(
                            "Success:%s...",
                            loginResult.getAuthorizationCode().substring(0,10));
                }

                // If you have an authorization code, retrieve it from
                // loginResult.getAuthorizationCode()
                // and pass it to your server and exchange it for an access token.

                // Success! Start your next activity...
                goToMyLoggedInActivity();
            }

            // Surface the result to your user in an appropriate way.
            Toast.makeText(
                    this,
                    toastMessage,
                    Toast.LENGTH_LONG)
                    .show();
        }
    }

10. Provide a Logout Button

If you began the login session with `AccountKitActivity.ResponseType.TOKEN`, a logout option is available to remove the stored AccessToken from the device.

import com.facebook.accountkit.AccountKit;

AccountKit.logOut();

11. Access Account Information on the Device

If your began the login session with AccountKitActivity.ResponseType.TOKEN, it's possible to access the Account Kit ID, phone number and email of the current account via a call to getCurrentAccount().
import com.facebook.accountkit.AccountKit;
import com.facebook.accountkit.Account;
import com.facebook.accountkit.PhoneNumber;
import com.facebook.accountkit.AccountKitCallback;
import com.facebook.accountkit.AccountKitError;

AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
  @Override
  public void onSuccess(final Account account) {
    // Get Account Kit ID
    String accountKitId = account.getId();

    // Get phone number
    PhoneNumber phoneNumber = account.getPhoneNumber();
    if (phoneNumber != null) {
      String phoneNumberString = phoneNumber.toString();
    }

    // Get email
    String email = account.getEmail();
  }
  
  @Override
  public void onError(final AccountKitError error) {
    // Handle Error
  }
});
    

12. Next Steps

To customize the behavior and appearance of your app, see the following topics: