Chapter 77 – Implement Linkedin Post Automation using Python and Linkedin API

In this article, I would walk through briefly how to implement linkedin post automation using Python and Linkedin API. By the end of this rich and juicy content details, you can learn how to create a Linkedin authentication token and post your first piece.

In this article, I would walk through briefly how to implement Linkedin post automation using Python and Linkedin API. By the end of this rich and juicy content details, you can learn how to create a Linkedin authentication token and post your first piece.

Tables of Content: Linkedin Post Automation using Python and Linkedin API

Linkedin API account page verification

First thing first, Linkedin requires us to have a company page verified to create an app. In this app creation, we like to get the app client ID and client secret for the upcoming step to generate auth token. So here are the things you need to prepare in advance

  • Linkedin company page
  • Privacy policy URL
  • App Logo

For the up-to-date requirement, please check out Linkedin developer interface.

https://www.linkedin.com/developers

Linkedin product activation

Then, we need to activate the Linkedin API products that need using for Linkedin post automation. Here are the three I use as usual

  • Share on LinkedIn
  • Sign In with LinkedIn
  • Sign In with LinkedIn V2

Please be sure to complete product activation before generating the auth token. It’s because the encrypted token scope would be based on the products you activated. To avoid going back and forward to generate a token, please keep in mind.

Auth token generation

There are many recommendations out there that require you to run a script using redirect URI for generating a token. In fact, this is the simplest way from my perspective which just requires you to click a few buttons. Here is the 2.0 tool you can leverage to generate the auth token.

Linkedin API endpoint and Author ID

For any post activity, Linkedin API endpoint requires the author ID. Here is the way to get your profile author ID which is verified above.

urlEndpoint = "https://api.linkedin.com/rest/me"

session.headers['Authorization'] = f"Bearer {token}"

session.headers['LinkedIn-Version'] = '202303'

API endpoint & Basic Post Format Automation

Now it’s time to try your first Linkedin post automation. Basically there are three main sections in the script. Here are the components as follows:

  • Post API endpoint
  • Post Body
  • JSON data post request

   urlEndPoint = "https://api.linkedin.com/v2/ugcPosts"

   session.headers['Authorization'] = f"Bearer {token}"

   post_body = {

       "author": "urn:li:person:<your author ID>",

       "lifecycleState": "PUBLISHED",

       "specificContent": {

           "com.linkedin.ugc.ShareContent": {

               "shareCommentary": {

                   "text": “Post body content”

               },

               "shareMediaCategory": "ARTICLE",

               "media": [

                   {

                       "status": "READY",

                       "description": {

                           "text": “post description”,

                       },

                       "originalUrl": “post landing url”,

                       "title": {

                           "text": “post title”,

                       }

                   }

               ]

           }

       },

       "visibility": {

           "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"

       }

   }

   response = session.post(urlEndPoint, json=post_body).json()

Full Python Scription of Linkedin Post automation if you are interested in alternative post format (Image, gif, video, etc)

If you are interested in Chapter 77 – Implement Linkedin Post Automation using Python and Linkedin API, please subscribe to our newsletter by adding the message ‘Chapter 77 + Linkedin api’. We would send you the script immediately to your mailbox.

I hope you enjoy reading Chapter 77 – Implement Linkedin Post Automation using Python and Linkedin API. If you did, please support us by doing one of the things listed below, because it always helps out our channel.