Linkedin API – Retrieve posts metrics not possible

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?

New contributor

Oscar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật