DEV Community

Cover image for LinkedIn API — OAuth 2.0 Access Token
Anlisha Maharjan
Anlisha Maharjan

Posted on • Originally published at anlisha.com.np on

LinkedIn API — OAuth 2.0 Access Token

Hi, on a recently worked project, we needed to fetch shares (posts) of company in LinkedIn. Thus, I write this step by step guide to access LinkedIn API.

The LinkedIn API uses OAuth 2.0 for user authorization and API authentication. Since we need access to Marketing APIs to fetch posts of company, we’ll follow Authorization Code Flow (3-legged authorization).

Step 1 — Register application in the LinkedIn Developer Portal

Create a new app in Developer Portal, specify App name, enter the LinkedIn Page of the company which will be associated with the application, upload logo and publish.

Note: It is required to associate the app with a LinkedIn Company Page. The company selected will function as the publisher of the app.

Step 2 — Request Application Verification

Go to Settings tab, select Verify button next to the company associated with the app. Click the Generate URL button in the Company Verification pop-up. Copy the URL and send it to the Page admin for verification.

Page admin has 30 days to verify the app with the unique URL. Once the Page admin approves the request, the app will show Verified in Settings tab. The link will only expire earlier than 30 days if Page admin clicks Deny in the request.

Step 3— Verify Application Request

As a Page admin when a unique URL is received requesting approval to associate the app with company Page; Clicking the URL will navigate to the Developer Portal. Sign in and the landing page will feature the request details; company page information, app information, and the requestor’s name. The request can either be approved or denied.

Once verification is complete, it cannot be undone. If the link is accessed after 30 days, the landing page will showcase an expired message and a new unique URL link must be generated.

Step 4— Application Credentials and Redirect URL

Next click the Auth tab to view application credentials i.e Client ID and Client Secret.

Under OAuth 2.0 settings, _we need to specify _Authorized redirect URLs. Since we’re using Postman to get access token, we’ll add the Postman callback URL ‘https://oauth.pstmn.io/v1/callback’ to the list.

Note: Please note Client ID and Client Secret for later use.

Step 5— Setup OAuth 2.0 scopes

To fetch data through LinkedIn API we requires access to proper OAuth 2.0 scopes such as r_basicprofile, r_organization_social. These scopes are based on the products added in the application.

Click the Products tab and apply to Marketing Developer Platform, Share on LinkedIn and Sign in with LinkedIn.

Complete the MDP access form under Products > Marketing Developer Platform. A confirmation email is received when the application has been reviewed.

Check the OAuth 2.0 scopes for the application now that products are added. Sign in with LinkedIn product gives access to r_liteprofile and r_emailaddress scopes. Share on LinkedIn product gives access to w_member_social scope. Remaining scopes are obtained from Marketing Developer Platform _product._

Step 6— Get Access Token via Postman

Open Postman, _Create _New Request _and Click _Authorization Tab. Select Type as OAuth 2.0. Fill in the given details:

  • Token Name : {provide token name}
  • Grant Type : Authorization Code
  • Callback URL: https://oauth.pstmn.io/v1/callback
  • Auth URL : https://www.linkedin.com/oauth/v2/authorization
  • Access Token URL : https://www.linkedin.com/oauth/v2/accessToken
  • Client ID : {provide Client ID}
  • Client Secret : {provide Client Secret}
  • Scope : r_basicprofile, r_liteprofile, r_emailaddress, w_member_social, r_organization_social, rw_organization_admin, w_organization_social
  • State : {provide a unique string value}
  • Client Authentication: Send client credentials in body

Click on Get New Access Token. _Postman will take us to the LinkedIn authorization page, where it is prompted to log into LinkedIn. Click _Allow to authorize the request. The prompt on the authorization page is dictated by the requested scopes in the previous step.

Postman will display the access token to be used for testing. Choose Use Token button to set this token.

Step 7— LinkedIn Find Shares By Owner API

Our application can now retrieve the collection of shares (posts) owned by a specific organization for which the authenticated member is an Admin.

API Endpoint:

GET https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:company:{id}&sortBy=LAST_MODIFIED&sharesPerOwner=1000&count=50&start=0&oauth2_access_token={ACCESS_TOKEN}

LinkedIn Company ID is a unique 6 to 9 digits long string of numbers. To find Company ID as a LinkedIn Page Admin, navigate to the Admin View of the LinkedIn Page from the All Pages or Home tab. The LinkedIn company ID is the numbers after “/company/” in the URL. For example, if the Admin URL for the LinkedIn Page is https://www.linkedin.com/company/8681905/admin/, the company ID is 8681905.

And this is it!

If you need to fetch shares (posts) of LinkedIn Company Page that doesn’t belong to you, then the Admin of that Company Page must add you as Content admin role. Skip these steps if not required.

As a Page Admin, access your LinkedIn Company Page Admin view. Click the Admin tools dropdown at the top of the page and select Manage Admins. Click the Add admin button. Search the authenticated member and assign role as Content admin and Save.

Dependencies:

  • For each company, the authenticated user must have a Admin role in the company’s LinkedIn Page to fetch shares (posts) of the organization.
  • The app must have access to Marketing Developer Platform product and its scopes.

References:

Getting Access to LinkedIn APIs – LinkedInThe LinkedIn API uses OAuth 2.0 for user authorization and API authentication. Applications must be authorized and…docs.microsoft.com

If you enjoyed learning about LinkedIn API access flow then feel free to give this article a clap and follow to stay up to date with more articles! Thanks for reading!

The post LinkedIn API — OAuth 2.0 Access Token first appeared on Anlisha Maharjan.

Top comments (0)