PostMaster (Part 4) Integrating LinkedIn in mobile apps

Aaditya Doshi
Novumlogic
Published in
8 min readOct 14, 2021

Welcome to the series of blog posts about PostMaster- A library for integrating social media accounts in mobile apps.

PostMaster library is a custom composer which enables its users to post on social media platforms such as Facebook, LinkedIn, Twitter, Instagram, and Telegram without opening and using the pre-installed app for each platform.

If you missed the previous parts of the blog series, you can find them here-

Part-1: Where I talked about how & why we decided to build PostMaster app, the problems we faced while building it, and tried and tested solutions for the same.

Part-2: Here, I talked in-depth about how to integrate Facebook in an android app and how we addressed the many issues faced while integrating the same.

Part-3: Here, I talked in-depth about how to integrate Twitter in an android app and how we addressed the many issues faced while integrating the same.

LinkedIn is one of the 5 social media platforms that we have integrated in the PostMaster library. So without any further ado let’s dive deep into how to integrated LinkedIn in mobile apps.

This article is divided in following parts-

  1. Steps required to add your app to LinkedIn Developer Portal
  2. Fetch Authorisation code to allow LinkedIn user login and request member permission
  3. Fetch an Access token needed for API requests directed to LinkedIn from your app
  4. Fetch the logged in LinkedIn user’s details
  5. Posting LinkedIn from your android app (with text and with text + image)

Let’s Begin!

1. Setup steps required to be done on LinkedIn Developer Portal-

  1. Create new project on LinkedIn developer portal by filling all the required details.
  2. After successful creation of the developer app, go to the “Auth tab” and copy the Client Id and Client Secret into your android app.
LinkedIn Developer Profile
LinkedIn Developer Profile- Auth Tab

Client Id and the Client Secret key will be used in almost all the API requests made to LinkedIn like for login or posting. Make sure you keep these keys well encrypted within your app. Check out our blog post here where we talk in-depth about how to keep important strings encrypted and very hard to decipher in an android app.

We have now done all the setup required to enable for us to write code to integrate LinkedIn in android app.

2. Steps for requesting the authorisation code

The LinkedIn API uses OAuth 2.0 for member (user) authorisation and API authentication. All applications must be authorised and authenticated before they can fetch data from LinkedIn or get access to LinkedIn member data. The authorisation code flow is used for applications to request permission from a LinkedIn member to access their account data.

Authorisation Code Flow

  1. Configure your application in the Developer Portal to obtain Client ID and Client Secret.
  2. Your application directs the browser to LinkedIn’s OAuth 2.0 authorisation page where the member authenticates.
  3. After authentication, LinkedIn’s authorisation server passes an authorisation code to your application.
  4. Your application sends this code to LinkedIn and LinkedIn returns an access token.
  5. Your application uses this token to make API calls on behalf of the member.

Request an authorisation code

To request an authorisation code, you must direct the member’s browser to LinkedIn’s OAuth 2.0 authorisation page, where the member either accepts or denies your application’s permission request.

After the request is made the user will be redirected to the Login page if it is a first time request else will be redirected to the redirect-URL defined in the parameter for the request.

Below is the details of the api and the parameters that needs to be passed to obtain the authorisation code

GET URL: https://www.linkedin.com/oauth/v2/authorization

Request Parameters:

scope — mentioned above refers to the permissions required to access user’s details such as name, email, profile pic etc and post content on user’s behalf. You can use these permission if your target audience is just members (LinkedIn users): r_liteprofile, r_emailaddress, w_member_social.

Points to be noted-

If your target audience includes organisations then you will have to apply for marketing developer platform API from developer profile.

You will have to open the above API in WebView as the API hit will lead to the login page of LinkedIn.

LinkedIn Login Screen for requesting authorisation code
LinkedIn login screen opened in web view after successfully requesting authorisation code

Code to call request authorisation code-

After successful attempt at login, a URL will be received containing the access code, extract that code and save it locally or on cloud as it will be further required to send requests on the logged in user’s behalf.

3. Request an access token

Access token plays a very important role for using the LinkedIn API as it contains all the details of the permissions that the user has granted to your app. Note- The access token will required to be passed in every API call henceforth.

Below are the details of the API URL and request parameters to obtain the access token.

GET URL: https://www.linkedin.com/oauth/v2/authorization

Request Parameters:

On success, you need to parse the json response fetched from above and extract the access token and store it locally/cloud.

4 (a). Fetch LinkedIn user details

To fetch the user’s details we will be using “v2/me” API endpoint of LinkedIn.

Below is the details of the API and the request parameters that needs to be passed to obtain the user’s details.

GET URL: https://www.linkedin.com/v2/me

Request Parameters:

Request Headers:

On success, you will receive a json response containing user’s first name, last name and links to profile picture of different resolutions. Parse the response and extract the values.

In the json response you will find a key-“id”, this refers to the user id, this id will be required for posting from your app to LinkedIn on behalf of the user

4 (b). Fetching user’s email

To fetch the user’s details we will be using “v2/emailAddress” API endpoint of LinkedIn.

Below is the details of the API and the request parameters that needs to be passed to obtain the user’s email.

GET URL: https://www.linkedin.com/v2/emailAddress

Request Parameters:

Request Headers:

On success, you will receive a json response containing user’s email address. Parse the response and extract the values.

LinkedIn user account listing

5. Posting on LinkedIn from your android app

Posting on LinkedIn is mainly of 2 types,

  1. Text post
  2. Image with text post

In both the mentioned types we will be using LinkedIn’s official APIs

Post with only text content

For creating post containing just text we will use “v2/ugcPosts” API endpoint of LinkedIn.

Below mentioned are the details of API required for creating text post.

POST URL: https://api.linkedin.com/v2/ugcPosts

JSON Request:

A json is required to be passed with this API along with user id and the text content that you want to post. User id is obtained from the API used to fetch the user details that we talked about in our 3rd section of this post.

Request Headers:

On success, you will get response code as 201 and a json response containing the post’s id.

Left — Screen showing custom post composer for a post, Right — Image showing the text post created from PostMaster on LinkedIn

Post with image and text

Posting image with text to LinkedIn is done in 3 steps-

  1. Register for image upload and get image upload link & media artifact
  2. Upload image to the link obtained in step 1
  3. Post caption for the image and media artifact after image is uploaded in step 2 to create post with image and text.

Step 1 : Registering for image upload

For register image upload we will use “v2/assets” API.

Below mentioned is the details of API required to register for image upload.

POST URL: https://api.linkedin.com/v2/assets?action=registerUpload

JSON Request:

Request Headers:

On success, you will get response code as 200 and a json response containing the upload URL and media artifact. You need to store these both in your app as these will be required in the next step

Step 2 : Upload image to server to the upload url

In this step we will upload the image to the upload URL obtained from step 1. Image that is required to create the image post on LinkedIn must be uploaded to the upload URL in binary form or as a byte array. Failing in doing so will lead to failure of the post creation.

Code to convert image to byte array and upload image-

Request Header:

On success, you will receive response code as 201 denoting the successful upload of image to LinkedIn servers.

Step 3: Creating post with image and text

After image is uploaded we will have to post caption for the text post. We will follow the same procedure as we did for creating text posts but with a little difference of json body that we will use in API.

Below mentioned is the details of api required to create the post with text and image.

POST URL: https://api.linkedin.com/v2/assets?action=registerUpload

JSON Request:

Request Headers:

On success, you will receive the response code as 201 denoting the creation of post with image and text.

Left — Screen showing custom post composer for a post with image, Right — Image showing the text with image posted on LinkedIn from PostMaster

Conclusion

Integrating LinkedIn was the most difficult part that we faced while building PostMaster. It took as a great amount of time and a good deal of trials & errors to achieve the above. I have tried to covered all the aspects of integration with LinkedIn into a mobile app in the hope of making this integration a smooth process for others. Stay tuned for the upcoming blogs on other social media platform integrations.

Also do give claps if this article was helpful.

Happy Coding !

--

--

Aaditya Doshi
Novumlogic

Mobile Developer | Graphic Designer | Tech Enthusiast | Caffeine Driven | Avid Learner | Marvel Cinematic Universe & Harry Potter Fan | Blues & Blues rock lover