LinkedIn API + Python = Programmatically Publishing  | by Alessandro Berti | Towards Data Science

I and other two friends of mine have a podcast and, each time we publish a new episode, we need to spread it around over socials (a.k.a. SPAM).
The repetitive action of copy&paste of the very same post over different socials, bother us a lot. So we decided to develop our web-app that let us deliver a single post over multiple platforms in one shot!
We smoothly succeeded in the integration of Facebook, Instagram, Twitter, and our Telegram channel.
We were very happy.
Then, LinkedIn came.
When you program, You would like to develop your application in the fastest way possible. Once it works, then you start refining the code.
This haste, however, necessarily leads to the loss of some important details hidden in docs. And guess what? Your code won’t work and you will go crazy.
After a few days, you find out the “why” it wasn’t working. Your reaction type can be clustered in two distinct sets:
You feel stupid. “It was evident! How could I not notice it?”
You get mad with LinkedIn because it is NOT admissible not providing a “ready-set-go” docs!
Which one do you belong to?
So,my mission is to present a super-practical-step-by-step guide to interacting with LinkedIn API through Python. That’s it.
Create your LinkedIn App on the LinkedIn Developers platform.Get the Access Token you need to incorporate in your Python app.Develop the script and make it works!
Create your LinkedIn App on the LinkedIn Developers platform.
Get the Access Token you need to incorporate in your Python app.
Develop the script and make it works!
Are you ready?
Go to the LinkedIn Developers platform.
www.linkedin.com
Then click on “Create app”
Provide the requested data
Verify your LinkedIn page by click on “Verify”
Click on “Generate URL” and follow the few steps requested by LinkedIn
Once done, if everything is OK, you’ll get the “Verified” on the main page of your app
Move to “Products” and enable both “Share on LinkedIn” and “Marketing Developer Platform” products
“Share on LinkedIn” authorizes you to manage your profile through LinkedIn API (i.e., in my case, the Alessandro Berti account)
“Marketing Developer Platform” authorizes you to manage your page through LinkedIn API (i.e., in my case, the PointerPodcast page)
⚠️ By clicking on “Marketing Developer Platform”, you’ll receive on your primary email, the following Request Form.⚠️
⚠️ You need to fill it, otherwise, LinkedIn isn’t going to authorize you to manage your page! ⚠️
Be patient, and wait for the answer. LinkedIn will contact you by email.
My request has approved in 2 days
You have successfully created and verified your app!
Easy, isn’t it?
Until we wait for the response, let’s go on with other stuff!
Now click on “Auth”. Here you find your Client ID and the Client Secret.
For the moment, just remember where they are.
Set up your “Authorized redirect URLs for your app”.
Be careful at the following syntax! /auth/linkedin/callback is mandatory!
https://example.com/auth/linkedin/callback
It’s time to get your hands dirty with some code!
To get your Access Token, you need two REST calls:
A GET that will redirect you to a new URL. From this URL, you’ll need to extrapolate the value in the parameter code.A POST that will return your brand new Access Token.
A GET that will redirect you to a new URL. From this URL, you’ll need to extrapolate the value in the parameter code.
A POST that will return your brand new Access Token.
Let’s do it step-by-step!
Just have a look at the following URL. Wait before copy&paste!
https://www.linkedin.com/oauth/v2/authorization? response_type=code&  client_id=YOUR_CLIENT_ID&  redirect_uri=https://example.com/auth/linkedin/callback&  state=RandomString&scope=w_member_social,r_liteprofile
Parameters
response_type=code. It’s already ok, just copy it.
cliend_id=YOUR_CLIENT_ID. Insert your Client ID specified in the Auth section of your LinkedIn App.
redirect_uri=https://example.com/auth/linkedin/callback. Insert the Authorized redirect URL you specified in the Auth section of your LinkedIn App.
state=RandomString. Chose your preferred-at-random “RandomString” (i.e. ILovePointerPodcast).
scope=w_member_social,r_liteprofile. The scope is very important! It specifies the permissions you need.
Let’s make it easy:
If you are planning to interact just with your own profile in “reading mode” then, the r_liteprofile permission is enough.
If you are planning to exploit LinkedIn API to publish posts in your own profile, you need “write permissions” (i.e. w_member_social).
However, you can compose permissions as in the example: scope=w_member_social,r_liteprofile.
If you are planning to use LinkedIn API to interact with your page, please keep reading until the ⚠️⚠️ ACHTUNG ⚠️⚠️ paragraph.
Permissions change according to your needs. In case you want to further investigate:
docs.microsoft.com
Now, you can copy&paste the following GET url.
Thanks for the patience.
https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=https://example.com/auth/linkedin/callback&state=RandomString&scope=w_member_social,r_liteprofile
Just paste the above link (filled with your correct data) in your browser.
If you are on the right way, you’ll get redirected here:
Once clicked on “Sign In”, you will get redirected again to an empty page!
⚠️ → Now, take a look at the new URL of the empty page! ← ⚠️
Copy it to a note. Is it something like that, right?
https://example.com/auth/linkedin/callback?code=YOUR_CODE&state=RandomString
Copy the value of the code parameter (i.e. YOUR_CODE); we need it to correctly invoke the POST.
The previously described GET allows you to manage ONLY your own profile account, NOT your page. To manage your page, you need to:
Wait for the approval by LinkedIn about the “Marketing Developer Platform”.
Include in the scope of the GET, the w_organization_social permission! (In case you intend to “read” data about your page, add also r_organization_social permission).
⚠️ If you try to invoke the following GET with the w_organization_social permission BUT you haven’t still received the approval to the “Marketing Developer Platform”, the GET will fail!⚠️
https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=https://example.com/auth/linkedin/callback&state=RandomString&scope=w_member_social,r_liteprofile,w_organization_social
Thus, you have to wait for the approval by LinkedIn.
Now, it’s POST call time! Let’s do it through bash!
curl -d "grant_type=authorization_code&code=YOUR_CODE&redirect_uri=https://example.com/auth/linkedin/callback&client_id=YOUR_CLIENT_ID&client_secret=YOUR_SECRET_ID" -H "application/x-www-form-urlencoded" -X POST "www.linkedin.com/oauth/v2/accessToken"
You already know what to do. Right?
Insert the YOUR_CODE obtained from the GET as the value of the code parameter.
Also, insert YOUR_CLIENT_ID and YOUR_CLIENT_SECRET. Just repeating: “You can find them in the “Auth” page of your LinkedIn App...”
Follows the POST response together with the Access Token!
If you requested the Access Token along with the w_organization_social permission, the above response will contain also a refresh_token field.
Copy the access_token field (and don’t lose it!)
From your LinkedIn App, click on “Auth” and then on “token inspector”.
Copy in the form your Access Token, then click “Inspect”.
Check that the permissions correspond to the ones you specified in the GET call.
If it’s like that, you have done!
To be able to interact with your profile, you need:
The Access Token (Who would have thought?)
Your profile ID
How can I get my profile ID? Let’s bash!
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -X GET "https://api.linkedin.com/v2/me"
Output:
The id field is... Guess what? Your Profile ID! In my case it is: idBfR6bf7d
docs.microsoft.com
docs.microsoft.com
To be able to interact with your page, you need:
The Access Token (along with w_organization_social)
Your page ID
How can I get my page ID? Just go on your page in “administrator view”
In my case it is: 42331509
docs.microsoft.com
This last script will work as soon as the review of the “Marketing Developer Platform” has been accepted by LinkedIn and you have re-generated a new access token with w_organization_social permission!
I’ve presented a very simple case, but since it works, you can start hacking it and try any other more specific LinkedIn API based on the python code provided!
Hope this super-practical-step-by-step-guide has been useful!
Any suggestions are super welcome! :)