Search code examples
linkedin-apimetricslinkedin-jsapi

LinkedIn API metrics


#linkedin

I am trying in python to get the metrics (likes, shares, comments, etc) of my company page. We registered the API which includes Marketing Developer Platform. I understand, I need Members authentication and I set up Oauth 2.0 settings. I've created the access_token for the permissions (among them is r_organization_social - which I need to collect data).

What is confusing for me, how to use this token. Is it enough to use it together with header in my request?

response = requests.get('https://api.linkedin.com/v2/posts', headers = headers)

I've created the header like this: headers = { 'Authorization': f'Bearer {access_token}', 'cache-control': 'no-cache', 'X-Restli-Protocol-Version': '2.0.0' }

I tried that and I am getting an error that token is Invalid, even though it is live and active for the next 11 months.

I also tried many things where I would first send request for authenticate my request and then manually copy the response to get the new access token. That access token never worked - was invalid. Even if it works, looks like that wouldn't be acceptable solution...

Can you please help with this?

Thanks


Solution

  • The following is working for me.

    I used this format of the header:

    headers = {
      'X-Restli-Protocol-Version': '2.0.0',  
      'Authorization': 'Bearer  '+ access_token } 
    payload ={}
    

    And the request looks like this (instead of posts I use ugcPosts):

    requests.request("GET",'https://api.linkedin.com/v2/ugcPosts?q=authors&authors=List('+urnLiOrganizationEncode + ')&count=100', headers=headers,data=payload)