Authenticating with the Mobile SDK for Android

The Mobile SDK for Android makes it easy for your application to request an access token that can be used to make API requests to LinkedIn on behalf of the mobile user.  Follow the steps below to integrate LinkedIn into your Android application:

Read the Getting Started guide

Before you begin, make sure that you have read the Getting Started with the Mobile SDK for Android guide.  It contains important information to know before you will be able to successfully authenticate a LinkedIn user within your mobile application.

Step 1 - Initialize a connection to LinkedIn

The LISessionManager class is the heart of the Mobile SDK for Android.  It provides all of the necessary functionality to create and manage the LISession object used to perform the additional Mobile SDK functions.

There are two ways to initialize a LinkedIn session using the overloaded init() method:

  • Without an existing access token
  • With a previously acquired access token

Initializing by requesting a new access token

If you do not have a previously serialized access token, this variation of init() will communicate with the official LinkedIn mobile application to request a new token for the current user. 

This process takes the user out of your application's flow and into the official LinkedIn Android application, resulting in several possible user experiences.  See Understanding the mobile authentication user experience for a break-down of what the user might encounter during this step of the mobile authentication process.

java
LISessionManager.init(Activity activity, Scope scope, AuthListener callback, boolean showGoToAppStoreDialog)

This variant takes four arguments:

  1. activity - A reference to an Android Activity within your application that you want the user to be returned to after the authentication process.
  2. scope - A list of LinkedIn member permissions that your application requires (e.g. r_basicprofile).
  3. callback - An implementation of AuthListener that will handle the results of the authentication process.  The onAuthSuccess() and onAuthError() methods should be overridden with your own versions to handle the result of the authentication process in a manner that makes the most sense for your application.
  4. showGoToAppStoreDialog - A boolean value that determines the behaviour when the LinkedIn app is not installed.  If true, a dialog is shown which prompts the user to install the LinkedIn app via the app store.  If false, the user is taken directly to the app store.

Below you will find an example of what the connection initialization portion of a typical authentication process might look like:

Java
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        // Store a reference to the current activity 
        final Activity thisActivity = this;

        LISessionManager.getInstance(getApplicationContext()).init(thisActivity, buildScope(), new AuthListener() {
            @Override
            public void onAuthSuccess() {
                // Authentication was successful.  You can now do
                // other calls with the SDK.
            }

            @Override
            public void onAuthError(LIAuthError error) {
                // Handle authentication errors
            }
        }, true);
    }
}

// Build the list of member permissions our LinkedIn session requires
private static Scope buildScope() {
    return Scope.build(Scope.R_BASICPROFILE, Scope.W_SHARE);
}

Initializing with an existing access token

If you have an access token for the current user that you have serialized from a previous interaction with your application, you can use it to create a new LISession object to work rather than requesting a brand new one.  Deserialize your access token and provide it as an argument to the following method:

java
LISessionManager.getInstance(getApplicationContext()).init(AccessToken accessToken);

Mobile vs. server-side access tokens

It is important to note that access tokens that are acquired via the Mobile SDK are only useable with the Mobile SDK, and cannot be used to make server-side REST API calls.

Similarly, access tokens that you already have stored from your users that authenticated using a server-side REST API call will not work with the Mobile SDK.

Step 2 - Handle responses from the LinkedIn mobile app

Making requests to the official LinkedIn app means that your application also needs to be setup to handle the resulting responses.  This is done by calling LISessionManager's implementation of onActivityResult() from within the calling activity's onActivityResult() method.

java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Add this line to your existing onActivityResult() method
    LISessionManager.getInstance(getApplicationContext()).onActivityResult(this, requestCode, resultCode, data);
}

Once your application is sending messages to and receiving messages from the LinkedIn mobile app successfully, your authentication workflow is complete.

Please refer to the sample application included in the Mobile SDK for Android download for a complete runnable example that demonstrates a successful end-to-end LinkedIn mobile authentication process.

Localization

To support our non-English speaking members, we have localized the mobile authorization screen.  Members who use SSO with LinkedIn in the 3rd party application see the authorization screen on the LinkedIn mobile app.  This screen is now localized to match the language setting on the member’s mobile device.

  • Localized authorization screen

Understanding the mobile authentication user experience

When LISessionManager.init() requiring a new access token to be requested is called, the SDK needs to communicate with the official LinkedIn application to retrieve that token for the current user.  During this process, there are several possible scenarios that could occur which result in different user experiences, based on:

  1. Is the LinkedIn application installed on the device?
  2. Is the user a LinkedIn member?
  3. Is the user signed into the LinkedIn application on their device?
  4. Has the user granted your application permission to access their profile?

LinkedIn app is not installed on the mobile device

When LISessionManager.init() is called, the SDK will direct the user to the device's app store page for the official LinkedIn application.  It is up the user to install it.  Once installed, the user will have to manually return to your application and restart the sign in process to continue.

LinkedIn app is installed, user is not a LinkedIn member

When LISessionManager.init() is called, the LinkedIn application will launch and the user will be prompted to create a new LinkedIn account.  The user will have to complete the account creation process (including email validation), manually return to your application and restart the sign in workflow to continue.

LinkedIn app is installed, user is not signed in to LinkedIn

When LISessionManager.init() is called, the LinkedIn application will launch and users will be prompted to login with their LinkedIn credentials.  Once validated, users will be presented with an authorization screen outlining the name of your application and displaying the permissions that your app is requesting.  If permission is granted, the user is returned to the Android Activity within your application that was provided in the argument to init().  A LISession object will now be available via LISessionManager.getSession() containing the access token required to perform further interactions with LinkedIn on the user's behalf.

LinkedIn app is installed, user is signed in to LinkedIn but has not authorized your app

When LISessionManager.init() is called, the LinkedIn application will launch and an authorization screen outlining the name of your application and displaying the permissions that your app is requesting. If permission is granted, the user is returned to the Android Activity within your application that was provided in the argument to init().  A LISession object will now be available via LISessionManager.getSession() containing the access token required to perform further interactions with LinkedIn on the user's behalf.

LinkedIn app is installed, user is signed in and previously authorized your app

When LISessionManager.init() is called, the LinkedIn application will immediately return the user back to the specified Android Activity within your application.  This will result in a brief screen flash as the device bounces out of your application, over to LinkedIn's application, then immediately back to yours.