Android LinkedIn Integration

Android LinkedIn Integration

Today we are going to learn how to integrate LinkedIn SDK in Android. By integrating LinkedIn into Android your user will be able to SingIn very quickly.

In this tutorial we are going to learn following features:

1. Authenticating with LinkedIn.

2. Fetching User Basic profile.

3. Sharing post to LinkedIn.

4. Opening current profile in LinkedIn app.

5. Opening others profile in LinkedIn app.

6. Log Out or clearing Logged In user session.

VIDEO DEMO

Create App in LinkedIn

First step is to create an app in LinkedIn. To create app follow the below video:

Now to get Package Hash for created app use the below code:

Place the above code inside your Activity and call it in onCreate and it will give you Package Hash which you can use to put inside created App settings.

Adding Package Name and Package Hash

Adding Package Name and Package Hash

Download Mobile SDK for Android

Now download LinkedIn mobile SDK from this link. After downloading follow the below video to see how to add the LinkedIn SDK in android studio project.

Authentication with LinkedIn

After successfully adding SDK into your Android Studio project use the below code to authenticate account with LinkedIn.

Now to handle the authentication response in your app use the below code:

LinkedIn Authentication

LinkedIn Authentication

LinkedIn Authentication Flow

During the authentication process, there are several possible scenarios that could occur which result in different user experiences, based on:

  1. LinkedIn app is not installed on the mobile device : When LISessionManager.init() is called, the SDK will re-direct the user to the device’s play store page for the official LinkedIn application. Once user installed the app, the user will have to manually return to your application and restart the sign in process to continue.
  2. 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.
  3. 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 authorisation 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.
  4. LinkedIn app is installed, user is signed in to LinkedIn but has not authorised your app : When LISessionManager.init() is called, the LinkedIn application will launch and an authorisation 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.
  5. LinkedIn app is installed, user is signed in and previously authorised 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.
Device doesn't have LinkedIn App

Device doesn’t have LinkedIn App

Fetching Basic Profile Data

After successful authentication you will be able to fetch user basic profile data by calling LinkedIn REST APIs. For fetching basic profile data you can use below code :

To know more about basic profile parameters check this link.

Successful Fetching of Basic Profile Data

Successful Fetching of Basic Profile Data

Share on LinkedIn

To share some data to LinkedIn you have to call another REST Api by posting the sharing content to it. You can use below code to share content:

NOTE : LinkedIn doesn’t provide Image Sharing via Gallery or Camera to his application via SDK for this you can use Share Intent.

Successful Content Share

Successful Content Share

Cancel API Calls

If you want to cancel any API call which is in-progress use the below code:

Open Current Profile

This method will open the currently authenticated user’s LinkedIn profile directly within the official LinkedIn mobile application. Use the below code to fetch current profile :

Open Current Profile

Open Current Profile

Open Other Profile

This method will open a specific member profile within the official LinkedIn application. The memberID is a value that is unique to your particular LinkedIn application, representing a specific LinkedIn member. You can retrieve the current user’s id by making a REST API call to LinkedIn’s Profile API using the SDK’s API wrapper method.

Storing member ID values on your own server allows your application to direct users to the official LinkedIn profiles for any other users that have authorised your mobile application.

To handle DeepLinking callback add the below code in your Activity :

Logout

You can clear/logout user session when you don’t want it anymore by calling clearSession method like below:

Example

1. Create an App on LinkedIn and get update Package Name and Package Hash over there. To know how to create App check the video given above in article.

2. After creating app download LinkedIn SDK and add it into your Android Studio project. Download the LinkedIn SDK from here.

3. After adding LinkedIn SDK and syncing project open activity_main.xml and add the below code to it. In this layout there are several buttons to perform different tasks like : SignIn, Share, Open Profile page and Logout.

You can download LinkedIn official button image to place background to your Button. Download Button image from here.

4. Now open MainActivity.java and place the below code to it. In this class every feature is there that we discusses above like SignIn, Share, Open profile page and Logout.

5. Finally all done , now you can also add LinkedIn integration into your app.

Thanks. :)

Download Source Code – >

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)