How to add ‘Sign In with LinkedIn’ Button to your Android app using Kotlin
- November 11, 2019
- John Codeos
Today, I’ll show you how to implement the ‘Sign In with LinkedIn’ button into your Android app.
We’re going to use the 3-legged OAuth method to get the authorization code and exchange it later for the access token. Using the access token, we’ll be able to get user’s information and email address making two HTTP GET requests, one to get user’s id, first name, last name and profile pic URL, and one to get the email address.
Lets get started!
First, create a LinkedIn account if you don’t already have one.
Go to https://www.linkedin.com/developers and click on the Create app button.
Fill in the required fields.
To create an app, you need to add your company’s page.
If you’re not a company or you want to avoid this step, just add a random company. It doesn’t cause any problem.
Next, go to the Auth tab of your app and add your redirect URL.
Creating the ‘Sign In with LinkedIn’ Button
First, add a Button to your XML file. In this example, we have a button in the middle of the activity_main.xml.
Create a new Kotlin object file by right-clicking on your app’s package name on the left side and go New > Kotlin File/Class.
Give it the name LinkedInConstants, choose Object and press OK.
Inside the new file, copy and paste the following code and replace the CLIENT_ID, CLIENT_SECRET and REDIRECT_URI with yours.
In your AndroidManifest.xml file, add internet permission.
In the build.gradle file of your project (ex. build.gradle (Project: LinkedInSignInExample)) add the following classpath
…and on your build.gradle file of your app module (ex. build.gradle (Module: app)) add the following.
In your Kotlin class of your Activity (In this example is the MainActivity), add a listener to your button, and create a Dialog with a WebView inside.
This WebView uses a custom WebViewClient, named LinkedInWebViewClient.
This helps to ‘catch’ the URL with the authorization code after the user has given access to your app.
After getting the authorization code, you need to exchange it for an access token by making an HTTP POST request to
After having the access token, you can get user’s info (Id, First Name, Last Name and Profile pic URL) and email address with two HTTP GET requests.
But before that, you need to create the Models.
Create a new Kotlin file in your project with a name LinkedInProfileModel and paste the following inside.
Do the same for the email, giving the name LinkedInEmailModel.
Now, get the User’s info
And user’s email address
You can find the final project here
If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!