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'.
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'.
4. Now, we need the 'Hash Key' for our app. It can be generated by two different ways.
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/ .
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.
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.
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.
Add the Internet permission in AndroidMenifest.xml file.
Add the linkedin-sdk in settings.gradle file.
Add the compile project(path: ':linkedin-sdk') in build.gradle file.
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.
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.
Now, add the following code in activity_profile.xml file. In this activity, we will display the user information after successful login.
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.