Javatpoint Logo
Javatpoint Logo

Integrating LinkedIn API in Android App

In this tutorial, we will integrate the LinkedIn Sign-In functionality in our Android application. Implementing LinkedIn API in Android app helps users to login using LinkedIn account, share post, etc.

For integrating the LinkedIn API in our Android app, we need the LinkedIn Authentication Key (Client ID and Client Secret) and app Hash Key.

Steps to generate LinkedIn Authentication Key and APP Hash Key

1. Create LinkedIn developer account at https://www.linkedin.com/developer/apps and click on 'Create Application'.


android LinkedIn Integrating

2. Fill all the required details of Android application in 'Create a New Application' form and accept the LinkedIn API Terms of Use then click 'Submit'.


android LinkedIn Integrating

3. After submitting the application details, it generates LinkedIn Authentication Key. Now we will select the 'Default Application Permissions'. This permission authorizes to access privilege of user account. Here, we are selecting r_basicprofile, and 'r_emailaddress' and click 'Update'.


android LinkedIn Integrating
android LinkedIn Integrating

4. Now, we need the 'Hash Key' for our app. It can be generated by two different ways.

  • Using command on command prompt.

Windows:

Mac/Unix

To generate the Hash Key through command prompt, it requires OpenSSL to be installed in our operating system. We can download it for Windows from www.slproweb.com/products/Win32OpenSSL.html and for Mac/Unix from http://www.openssl.org/source/ .

  • Using the programming code in Activity.java class.

For this application, we will use the programming code to generate 'Hash Key' for our application.

Now create our application and simply write the following code and run to generate our 'Hash Key'. It will show the application 'Hash Key' in Logcat.

MainActivity.java


android LinkedIn Integrating

5. Copy the 'Hash Key' from Logcat and paste it in Mobile tab of our LinkedIn application. After that click on 'Add' and 'Update'. This makes our application 'Hash Key' registered with LinkedIn API.


android LinkedIn Integrating

Example to Integrate LinkedIn Login in Android app

Let's create an example of integrating the LinkedIn log-in functionality in our Android application. After successful user log-in, it will redirect user to another activity (ProfileActivity) and display the user information.

We need to add the LinkedIn SDK for Android in our project. It can be downloaded from here https://developer.linkedin.com/downloads#androidsdk.


android LinkedIn Integrating

Required Permission

Add the Internet permission in AndroidMenifest.xml file.

settings.gradle

Add the linkedin-sdk in settings.gradle file.

build.gradle (Module)

Add the compile project(path: ':linkedin-sdk') in build.gradle file.

activity_main.xml

Add the following code in activity_main.xml file. Download the recommended LinkedIn button from LinkedIn developer site https://developer.linkedin.com/downloads and add it as the background of button.

MainActivity.java

In the MainActivity.java class, we use the LISessionManager class which provides all functionality to create and manage the LISession object (LinkedIn session object). In the build, scope adds the 'Scope.R_BASICPROFILE' and 'Scope.R_EMAILADDRESS' to access user basic profile information and email address of LinkedIn.

activity_profile.xml

Now, add the following code in activity_profile.xml file. In this activity, we will display the user information after successful login.

ProfileActivity.java

Add the LinkedIn API URL https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address) in getRequest() method of APIHelper class. It will retrieve the user information on success of LinkedIn API.

Output:

android LinkedIn Integrating android LinkedIn Integrating
android LinkedIn Integrating android LinkedIn Integrating
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials