Android Linkedin Login Integration and Post Messages in Linkedin profile

Koushik Mondal
4 min readJul 30, 2019

LinkedIn integration for android application is quite complex after changing the process of Linkedin login integration. I have found the previous android SDK for Linkedin also not working and there is no latest GitHub library for android Linkedin integration. I think this blog will definitely help people who are looking for a simple process of LinkedIn integration to get user profile and post a message in Linkedin profile.

Android-LinkedIn-Login-Post-Integration:

This library will help to integrate LinkedIn login in android application to get user lite profile with email and post image, message and article in the LinkedIn profile.

How to setup:

It’s very easy to use this library as it has two different listeners for login and post message for Linkedin.

Add to Gradle:-

Add this to your project-level build.gradle file.

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

And then add this to your module-level build.gradle file

dependencies {
implementation 'com.github.koushikcse:LinkedIn:Tag'
}

How it works:

In your application inside onCreate override method add following line to initialize Linkedin with required details. If you only want user lite profile and email for Linkedin login integration then provide permission scope=listOf("r_liteprofile", "r_emailaddress") or if you want to post a message with image/article link then provide permission scope=listOf("r_liteprofile", "r_emailaddress","w_member_social"). For initializing this library we need to provide the following details. CLIENT_ID , CLIENT_SECRET and REDIRECT_URL of your Linkedin application.

Kotlin

Linkedin.initialize(
context = applicationContext,
clientId = "CLIENT_ID",
clientSecret = "CLIENT_SECRET",
redirectUri = "REDIRECT_URL",
state = "RANDOM_STRING",
scopes = listOf("PERMISSION_OPTIONS")
)

Java

Linkedin.Companion.initialize(getApplicationContext(),
"CLIENT_ID",
"CLIENT_SECRET",
"REDIRECT_URL",
"RANDOM_STRING",
Arrays.asList("PERMISSION_OPTIONS")
);

Setup Login Listener:-

Then in your activity on button click add these lines. In LinkedinLoginListener callback you will get failed and success listener override methods. For success Linkedin login you will get user details and token in SocialUser object.

Kotlin

btnlogin.setOnClickListener {
Linkedin.login(this, object : LinkedinLoginListener {
override fun failedLinkedinLogin(error: String) {
//todo failed functionality
}

override fun successLinkedInLogin(socialUser: SocialUser){
// todo success functionality
}
})
}

Java

btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Linkedin.Companion.login(LinkedinActivity.this, new LinkedinLoginListener() {
@Override
public void successLinkedInLogin(@NotNull SocialUser socialUser) {
// todo success functionality
}

@Override
public void failedLinkedinLogin(@NotNull String s) {
//todo failed functionality
}
});
}
});

Setup Message Post Listener:-

In LinkedinLoginListener for successful login, you will get socialUser object, from which you will get accessToken and socialId. Then you can post a message in LinkedIn profile with these accessToken and socialId. And here you can set visibility of the message, providing true for visibility your message will be publicly visible otherwise it will be visible only for connections of that user. There are three methods for posting a message in the LinkedIn profile. For normal message call postMessage, for posting a message with article link call postArticle method and for posting an image with text in LinkedIn profile call postImage method. Check the following uses of these methods for better understanding.

Post Normal Text Message

Kotlin

Linkedin.postMessage(
"MESSAGE",
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
object : LinkedinPostResponseListner {
override fun linkedinPostSuccess() {
// todo success functionality
}

override fun linkedinPostFailed(error: String) {
//todo failed functionality
}
})

Java

Linkedin.Companion.postMessage(
"MESSAGE",
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
new LinkedinPostResponseListner() {
@Override
public void linkedinPostSuccess() {
// todo success functionality
}

@Override
public void linkedinPostFailed(@NotNull String s) {
//todo failed functionality
}
});

Post Article With Text Message

Kotlin

Linkedin.postArticle(
"MESSAGE",
"ARTICLE_URL"
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
object : LinkedinPostResponseListner {
override fun linkedinPostSuccess() {
// todo success functionality
}

override fun linkedinPostFailed(error: String) {
//todo failed functionality
}
})

Java

Linkedin.Companion.postArticle(
"MESSAGE",
"ARTICLE_URL",
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
new LinkedinPostResponseListner() {
@Override
public void linkedinPostSuccess() {
// todo success functionality
}

@Override
public void linkedinPostFailed(@NotNull String s) {
//todo failed functionality
}
});

Post Image With Text Message

Kotlin

Linkedin.postImage(
"MESSAGE",
"IMAGE_FILE"
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
object : LinkedinPostResponseListner {
override fun linkedinPostSuccess() {
// todo success functionality
}

override fun linkedinPostFailed(error: String) {
//todo failed functionality
}
})

Java

Linkedin.Companion.postImage("MESSAGE",
"IMAGE_FILE",
true,
"ACCESS_TOKEN",
"SOCIAL_ID",
new LinkedinPostResponseListner() {
@Override
public void linkedinPostSuccess() {
// todo success functionality
}

@Override
public void linkedinPostFailed(@NotNull String s) {
//todo failed functionality
}
});

And that’s it! If you enjoyed reading this, please click the clapping icon below. This will help to share the story with others.

Reference: https://github.com/koushikcse/LinkedIn

--

--

Koushik Mondal

Senior Software Engineer - [Android | Kotlin | Java] at Innofied