Linkedin Auth Flow using Python

Pius Adams Ijachi
4 min readJan 25, 2021

So recently in a bid to automate the creating posts on linkedin , I started reading about the linkedin api and honestly wasn’t a pretty experience maybe its just me but i found the documentation a little hard to follow especially for a beginner…. So this article is meant to help you understand the processes and also create a post…lets get to it !!!.

First off we’ll need to create an app in the linkedin devolpers platform

linkedin dev homepage

then click on create app

create app form

fill the form with the required details… if you don’t have any you will need to create a linkedin page (very simple process) and add a logo for it (any pic will do)

After filling the form click on create app

screenshot by author

click on verify

screenshot

copy the verification url ,open a new tab and paste the url and go to the link then verify

screenshot

Ok now you are verified next click on the products page at the top, click on select next to the SHARE ON LINKEDIN and SIGN IN ON LINKEDIN

we want those two because SHARE ON LINKEDIN provide us with some permissions needed to make share a post (basically create a post) and SIGN IN ON LINKEDIN provides us with permission to get user info (we will need user id ,this gives us access) the Marketing Developer Platform will give more access and for how to use it I suggest reading this article but we don’t need it for what we are doing

after clicking your products page should look like this

screenshot

Now go to the Auth page and scroll down to the OAuth 2.0 scopes

screenshot

The scopes here are the permission we have available ,The SHARE ON LINKEDIN gave us the w_member_social (to create modity and delete post) and the SIGN IN WITH LINKEDIN gave us the r_emailaddress and r_liteprofile( which you would use for social auth and we will use to get profile_id)

Next up we setup out Redirect url

https://example.com/auth/linkedin/callback

if you have website replace example.com with your site name and if its running locally like for this project we need a PYTHON HTTP WEBSERVER (we’ll do that later) for now add

screenshot

SO now its time to open up those code editors !!!

We need to get the Access Token and that’s just two steps

  1. We send a GET request to get a code needed and also for the user to authenticate

so before you copy and paste lets quickly go over it and add webbrowser.open(url) to the code…

client_id = this is the id from your auth page on linkedin
redirect url = this should be the same url you put
response_type = code #its a fixed value
scope = this are all the permission we need
state = this is just a unique identifer

Thanks to stackoverflow for that but before we run this code we need to start our webserver so it has somewhere to redirect to for that we need to create a HTTP PYTHON WEBSERVER (if you were using a framework it would just redirect there)

After running that we can now run the get for the linkedin code you should get a redirected to a page..

screenshot

copy the code (all of it ) and it’s only valid for 30minutes

2. POST we need to send a post requesting the access code using the code we just copied

replace code with the code you just got and run it you should get

With the token we can send a get request to retrieve user info (what we want is the ID)

Put the access_token you got and run ,copy the ID after and now we can now make posts !!!!!!

SO create a text.txt file in the same directory and write what you want to post ,replace profile_id with the id you and run……. your post should have been made…

I HOPE THIS HELPS !!!!!! here is the github repo

--

--