Account Kit for Android
To set up Account Kit in your Android app, follow these steps:
1. Prerequisites
2. Configure the SDK
3. Initialize the SDK
4. Check for Existing Sessions
5. Initiate a Login flow for SMS
6. Initiate a Login flow for Email
7. Perform Additional Configuration
8. Handle the Activity's Result
9. Provide a Logout Button
10. Access Account Information on the Device
Also 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.
1. Prerequisites
Before you begin integrating Account Kit into your app, make sure you have completed the following prerequisites.
A. Create a Developer Account
If you don't have a Facebook developer account, create one by clicking the button below.
Your Facebook developer account gives you access to developer tools and allows you to create Facebook apps.
Already have a Facebook developer account? Skip to the next step.
Create Developer AccountB. Get a Facebook App ID
Account Kit for Android requires a Facebook app ID. Follow the steps in the Getting Started, or to use Quick Start, click the button below.
Quick Start for AndroidC. Download the Account Kit SDK
Download the latest Facebook SDK for Android and use it to integrate your app with Account Kit.
Download the Android SDK2. Configure the SDK
Add your Facebook app ID and your Account Kit client token to the AndroidManifest.xml file. You'll find the Account Kit client token in the Account Kit section of the App Dashboard. The application name will be used in the UI on the login screen.
Add the compile dependency with the latest version of the Account Kit SDK in the build.gradle file:
repositories {
jcenter()
}
dependencies {
compile 'com.facebook.android:account-kit-sdk:4.+'
}
Add the following to the AndroidManifest.xml
<meta-data android:name="com.facebook.accountkit.ApplicationName"
android:value="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/FACEBOOK_APP_ID" />
<meta-data android:name="com.facebook.accountkit.ClientToken"
android:value="@string/ACCOUNT_KIT_CLIENT_TOKEN" />
<activity
android:name="com.facebook.accountkit.ui.AccountKitActivity"
android:theme="@style/AppLoginTheme"
tools:replace="android:theme"/>
Define the value for FACEBOOK_APP_ID as the Facebook app ID shown at the top of your application dashboard, and the value for ACCOUNT_KIT_CLIENT_TOKEN using the client token found in the Account Kit tab in the App Dashboard.
The AccountKitActivity must be defined here as well, to enable it to start in the app. Set the android:theme attribute here to customize the color scheme of the UI.
If you wish to disable App Events for your Account Kit application, add the following line to AndroidManifest.xml:
<meta-data android:name="com.facebook.accountkit.FacebookAppEventsEnabled"
android:value="false"/>
By default, this value is true. See App Events and Analytics for more information.
To reduce the size of the SDK, you can specify only the supported languages you need. See Specifying Supported Languages on how to do this.
3. Initialize the SDK
In your Main Activity or in your application object, initialize Account Kit.
import com.facebook.accountkit.AccountKit;
@Override
public void onCreate() {
super.onCreate();
AccountKit.initialize(getApplicationContext());
}
4. 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.
5. Initiate a Login flow for SMS
import com.facebook.accountkit.AccountKit;
public static int APP_REQUEST_CODE = 99;
public void onLoginPhone(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.
6. Initiate a Login flow for Email
import com.facebook.accountkit.AccountKit;
public static int APP_REQUEST_CODE = 99;
public void onLoginPhone(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_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.
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 following intent filter to your AndroidManifest.xml file:
<activity android:name="com.facebook.accountkit.ui.AccountKitActivity">
<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>
7. Perform Additional Configuration
The AccountKitConfigurationBuilder object offers multiple UI and behavior customization points for your use of Account Kit via methods that allow you to override default properties at runtime.
| Method | Description |
|---|---|
| (Optional) A developer-generated nonce used to verify that the received response matches the request. Fill this with a random value at runtime; when the login call returns, check that the corresponding param in the response matches the one you set in this method. |
| (Optional) Pre-fill the user's email address in the email login flow. Note: By default, the email field provides a dropdown list of the user's email addresses if the |
| (Optional) Set the default country code shown in the SMS login flow. |
| (Optional) Pre-fill the user's phone number in the SMS login flow. |
| (Optional) If this flag is set, Account Kit offers the user the option to receive their confirmation message via a Facebook notification in the event of an SMS failure, if their phone number is associated with their Facebook account. The associated phone number must be the primary phone number for that Facebook account. Default: |
| (Optional) Set to Default: |
| (Optional) Pass in a resource identifier for a theme to have that theme used for the login screen. See Customizing the UI for Android for more information. Note: If you specify both a theme and an Advanced UI Manager object (see below), the Advanced UI Manager will take precedence and overwrite theme elements. |
| (Optional) Pass in an object that implements the |
| (Optional) If the Default: |
| (Optional) If the Default: |
| (Optional) Use this to specify a list of permitted country codes for use in the SMS login flow. The value is an array of short country codes as defined by ISO 3166-1 Alpha 2. To restrict availability to just the US (+1) and The Netherlands (+31), pass in |
| (Optional) Use this to specify a list of country codes to exclude during the SMS login flow. Only the country codes in the blacklist are unavailable. People can still use the rest of Account Kit's supported country codes. If a country code appears in both the whitelist and the blacklist, the blacklist takes precedence and the country code is not available. Just like the whitelist, the value is an array of short country codes as defined by ISO 3166-1 Alpha 2. |
When you whitelist and blacklist country codes, you can use the following combinations of lists with the described results.
| Lists | Result |
|---|---|
No whitelist or blacklist | All country codes supported by Account Kit are available. |
Whilelist | Only country codes in the whitelist are available. |
Blacklist | All country codes supported by Account Kit except those in the blacklist are available. |
Whitelist and blacklist | Only the country codes in the whitelist that are not also in the blacklist are available. Note that the blacklist takes priority for codes that that are in both lists. |
8. 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.
@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();
}
}
9. 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();
10. 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().
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();
String phoneNumberString = phoneNumber.toString();
// Get email
String email = account.getEmail();
}
@Override
public void onError(final AccountKitError error) {
// Handle Error
}
});