I am trying to retrieve linkedin API data via python for the company’s page posts and related metrics (shares, likes, etc.), but it doesn’t seem to work although I followed the developers documentation.
This is my code:
import requests
import pandas as pd
import time
def fetch_linkedin_posts(access_token, organization_id):
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# URL to get posts for an organization
url = f'https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:{organization_id}&sharesPerOwner=100'
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data.get('elements', [])
else:
print(f"Error fetching posts: {response.status_code}, {response.json()}")
return []
def fetch_post_metrics(access_token, post_urn):
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# URL to get post statistics
url = f'https://api.linkedin.com/v2/organizationalEntityShareStatistics?q=organizationalEntity&organizationalEntity={post_urn}'
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data.get('elements', [])
else:
print(f"Error fetching metrics for post {post_urn}: {response.status_code}, {response.json()}")
return []
access_token = 'XXX'
organization_id = 'XXX'
# Fetch LinkedIn posts
posts = fetch_linkedin_posts(access_token, organization_id)
if posts:
post_data = []
for post in posts:
post_info = {
'post_id': post.get('id'),
'text': post.get('text', {}).get('text', ''),
'creation_date': pd.to_datetime(post.get('created', {}).get('time'), unit='ms')
}
post_urn = post.get('activity')
metrics = fetch_post_metrics(access_token, post_urn)
if metrics:
metric_data = metrics[0] # Assuming only one metric element for each post
post_info['likes'] = metric_data.get('totalSocialActivityCounts', {}).get('likeCount', 0)
post_info['shares'] = metric_data.get('totalSocialActivityCounts', {}).get('shareCount', 0)
post_info['comments'] = metric_data.get('totalSocialActivityCounts', {}).get('commentCount', 0)
post_info['impressions'] = metric_data.get('totalImpressionCount', 0)
post_data.append(post_info)
# Sleep to avoid hitting API rate limits
time.sleep(1)
# Convert to DataFrame
df = pd.DataFrame(post_data)
# Print the DataFrame
print(df)
else:
print("No posts found or failed to retrieve posts")
However, when running the query I get this main error for some posts:
{‘serviceErrorCode’: 100, ‘message’: ‘Field Value validation failed in PARAMETER: Data Processing Exception while processing fields [/organizationalEntity]’, ‘status’: 403}
and for other posts, only the text is being generated in the dataframe, but not the metrics I’m looking for. In the linkedin app I have the Community Management API activated in order to query posts data, as according to documentation. And in the page I am a super admin, so I should be able to query all data, not sure what is going on. Can anyone help me out? Am I missing something?
Oscar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.