Overview
Posting to the LinkedIn API in Python allows you to automate posting content to LinkedIn profiles and pages. The LinkedIn API provides several endpoints for creating posts, uploading images and videos, and publishing long-form articles.
To post to the LinkedIn API in Python, you need to:
- Register as a LinkedIn developer and create an app to get API credentials
- Install the Python LinkedIn API wrapper library
- Authenticate with OAuth 2.0 to get access tokens
- Construct the API request with headers, parameters, and data
- Make the API call to create the post
In this comprehensive guide, we’ll cover:
- Registering as a LinkedIn developer and getting API keys
- Installing the Python LinkedIn API wrapper
- Authenticating with OAuth 2.0
- Constructing the API request
- Creating text, image, video, and article posts
- Best practices and limitations of the LinkedIn API
Register as a LinkedIn Developer
The first step is to register as a LinkedIn developer to get API credentials for your app. Here is how:
- Go to LinkedIn Developers and sign in with your LinkedIn account.
- Click on My Apps in the top menu bar.
- Click Create Application.
- Fill in the form:
- Application Name – Name of your app.
- Application Description – Brief description of what your app does.
- Application Logo – Optional logo image for your app.
- Application Use – Select “Personal Use” if this is just for your own use.
- Website URL – Your website or GitHub URL.
- OAuth 2.0 Redirect URLs – This can be left blank for now.
- Legal Agreement – Check the box to agree to the terms.
- Click Submit.
Once your LinkedIn app is created, you can find the client ID and client secret under the Auth section. You’ll need these values later to authenticate with OAuth 2.0.
Install LinkedIn API Wrapper
To call the LinkedIn API in Python, we can use the linkedin-api wrapper library. It wraps the LinkedIn REST and Stream APIs to make it easier to integrate LinkedIn data into Python applications.
Install the library with pip:
“`
pip install linkedin-api
“`
This will download and install the linkedin-api package so we can import it in our Python code.
Authenticate with OAuth 2.0
To make authenticated requests to the LinkedIn APIs, we need an access token. LinkedIn uses the OAuth 2.0 protocol for authentication and authorization.
Here are the steps to authenticate and get an access token:
- Import the Linkedin class from linkedin_api.
- Construct a Linkedin instance with your API credentials:
“`python
API_KEY = ‘YOUR_CLIENT_ID’
API_SECRET = ‘YOUR_CLIENT_SECRET’linkedin = Linkedin(API_KEY, API_SECRET)
“` - Generate the OAuth 2.0 authentication URL:
“`python
print(linkedin.get_auth_url())
“`This prints a URL to request OAuth access.
- Visit the printed authentication URL in your browser. Approve access to your LinkedIn account.
- After approval, you will be redirected to a blank page with a URL containing the `code` query parameter.
- Pass this `code` to the `get_access_token` method:
“`python
code = ‘CODE_FROM_REDIRECT’access_token = linkedin.get_access_token(code)
“`This will exchange the code for an access token. The `access_token` can now be used to make authenticated API calls.
“`python
from linkedin_api import Linkedin
“`
We now have an access token to post to LinkedIn as the authenticated user. The token should be refreshed frequently.
Construct API Request
With the API wrapper and access token, we can now construct requests to post content to LinkedIn.
The linkedin-api library provides several methods to create posts, upload multimedia, and publish articles.
Here is an example to create a simple text share post:
“`python
# Import Linkedin class
from linkedin_api import Linkedin
# Create Linkedin instance with credentials
linkedin = Linkedin(API_KEY, API_SECRET)
# Get access token
access_token = linkedin.get_access_token(code)
# Construct post data
post_data = {
“comment”: “Check out my new article on posting to the LinkedIn API!”,
“visibility”: {
“com.linkedin.ugc.MemberNetworkVisibility”: “PUBLIC”
}
}
# Set headers
headers = {‘Authorization’: ‘Bearer ‘ + access_token}
# Post share and print response
response = linkedin.post_share(post_data, headers=headers)
print(response)
“`
The steps are:
1. Create LinkedIn API instance with credentials
2. Get access token
3. Construct data for the post (comment, visibility etc.)
4. Set headers with access token
5. Call post_share method to create post
6. Print API response
This will publish a share post to the authenticated user’s LinkedIn profile.
We can post other content types by calling the corresponding API method:
* post_article() – Publish a long-form article
* post_image() – Post an image with caption
* post_video() – Post a video with description
Each content type has its own parameters that can be passed. Refer to the LinkedIn API docs for details.
Post a Text Share
Let’s look at another example to post a text share:
“`python
post_data = {
‘author’: ‘urn:li:person:{linkedin_id}’,
‘lifecycleState’: ‘PUBLISHED’,
‘specificContent’: {
‘com.linkedin.ugc.ShareContent’: {
‘shareCommentary’: {
‘text’: ‘Hello World! This is my first Share using the LinkedIn API in Python!’
},
‘shareMediaCategory’: ‘NONE’
}
},
‘visibility’: {
‘com.linkedin.ugc.MemberNetworkVisibility’: ‘PUBLIC’
}
}
response = linkedin.post_share(post_data, headers)
“`
Here we are:
– Setting the author as the LinkedIn member ID
– Setting visibility to public
– Specifying the share text commentary
– Not including any media
This will publish a public text share to the member’s connections.
Post an Article
To publish a long-form article, we can use the post_article() method:
“`python
article_data = {
‘author’: ‘urn:li:person:{linkedin_id}’,
‘lifecycleState’: ‘PUBLISHED’,
‘specificContent’: {
‘com.linkedin.ugc.ShareContent’: {
‘shareCommentary’: {
‘text’: ‘Check out my new article!’
},
‘shareMediaCategory’: ‘ARTICLE’,
‘media’: [
{
‘status’: ‘READY’,
‘description’: {
‘text’: ‘Article description’
},
‘originalUrl’: ‘https://example.com/article’,
‘title’: {
‘text’: ‘Article headline’
}
}
]
}
},
‘visibility’: {
‘com.linkedin.ugc.MemberNetworkVisibility’: ‘PUBLIC’
}
}
response = linkedin.post_article(article_data, headers)
“`
Here we:
– Set the shareMediaCategory to ARTICLE
– Pass the article details like title, URL, and description
– Set visibility to public
This will publish an article post with the provided metadata.
Post an Image
To upload an image, we first need to get an upload URL via the `get_image_upload` method:
“`python
upload_url = linkedin.get_image_upload(headers)
“`
This returns a URL where we can upload the image binary data.
Then we can upload the image and get an asset ID:
“`python
image = open(‘/path/to/image.png’, ‘rb’).read()
asset = linkedin.upload_image(image, upload_url, headers)
asset_id = asset[‘value’][‘asset’]
“`
With the image uploaded, we can construct a post request:
“`python
image_data = {
‘author’: ‘urn:li:person:{linkedin_id}’,
‘lifecycleState’: ‘PUBLISHED’,
‘specificContent’: {
‘com.linkedin.ugc.ShareContent’: {
‘shareCommentary’: {
‘text’: ‘Check out this cool photo!’
},
‘shareMediaCategory’: ‘IMAGE’,
‘media’: [
{
‘identifier’: asset_id,
‘status’: ‘READY’
}
]
}
},
‘visibility’: {
‘com.linkedin.ugc.MemberNetworkVisibility’: ‘PUBLIC’
}
}
response = linkedin.post_image(image_data, headers)
“`
We pass the asset ID of the uploaded image as the media identifier.
This will create an image share post on LinkedIn with the photo.
Post a Video
Video posts work similarly to images.
First get a video upload URL:
“`python
upload_url = linkedin.get_video_upload(headers)
“`
Upload the video and get the asset ID:
“`python
video = open(‘video.mp4’, ‘rb’).read()
asset = linkedin.upload_video(video, upload_url, headers)
video_id = asset[‘value’][‘asset’]
“`
Then construct the video post request:
“`python
video_data = {
# …
‘specificContent’: {
‘com.linkedin.ugc.ShareContent’: {
# …
‘media’: [
{
‘identifier’: video_id,
‘status’: ‘READY’
}
]
}
}
# …
}
response = linkedin.post_video(video_data, headers)
“`
This will share the video in a LinkedIn post.
Best Practices
Here are some tips when posting to the LinkedIn API:
- Refresh access tokens frequently – Set up a process to refresh tokens to avoid failures.
- Check response codes – Print and check API responses for errors.
- Set up rate limiting – Avoid hitting LinkedIn API limits by adding delays.
- Post high-quality content – Follow LinkedIn’s guidelines for spam and quality.
- Add relevant metadata – Optimize posts with images, videos, tags where appropriate.
Limitations
Some current limitations of the LinkedIn API include:
- No automated engagement – You cannot automatically like or comment on posts.
- No InMail – The InMail messaging API is restricted.
- No content targeting – Posts cannot be targeted to specific audiences.
- No multimedia management – No API for managing multimedia assets.
So you cannot use the API for automated engagement or messaging. And content distribution is limited.
Conclusion
Posting to LinkedIn’s API opens up many possibilities for marketers, recruiters, and developers to integrate LinkedIn functionality into their applications.
The key steps are:
- Register as a LinkedIn developer
- Install the LinkedIn API wrapper
- Authenticate with OAuth 2.0
- Construct and make API calls
With the API, you can create posts, share articles, upload multimedia, and more.
Make sure to follow best practices around tokens, rate limiting, and content quality.
While the API has some restrictions, it enables automated publishing of content on LinkedIn at scale.