Getting LinkedIn company shares Python

Viewed 251

I am trying to get company shares in Python with the requests library. My app has the additional Marketing Developer Platform access and I am a super admin for the company page I am trying to get shares from. The authorsation described here works fine. I can successfully place a https://api.linkedin.com/v2/me GET request.

Based on this tutorial, the LinkedIn documentation and this suggestion I wrote the following code using the LinkedIn test organisation ID:

def get_posts(access_token):
    URL = "https://api.linkedin.com/v2/shares"
    headers = {'q':'owners', 'owners': 'urn:li:organization:2414183',
        'Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
    response = requests.get(url=URL, headers=headers)
    print(response.json())

get_posts(access_token)

The error code is {'serviceErrorCode': 0, 'message': 'Resource shares does not exist', 'status': 404}

The error message remains the same when using the actual company ID (9481327). The answer to this question does not provide any code or hint for above problem. This question is based on the V1 api, which is now depreceated.

Up-date 30/05/2022 - below function finds the resource, but cannot process the parameters.

def get_comments(acccess_token):
    URL = 'https://api.linkedin.com/v2/shares'
    PARAM = {'q':'owners', 'owners':'urn:li:organization:2414183', 'sortBy':'LAST_MODIFIED',
    'sharesPerOwner':"100"}
    headers = {'Content-Type': 'application/x-www-form-urlencoded',
               'Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
    response = requests.get(url=URL, params = PARAM, headers=headers)
    print(response.json())
    
get_comments(access_token)

{'message': 'Invalid value type for parameter owners', 'status': 400}
The error message is the same for LinkedIn's test page (2414183) and the actual company page I want to access (9481327)

Up-date 01/06/2022 using the ugcPost API provides a similar error message

def get_comments(acccess_token):
    URL = 'https://api.linkedin.com/v2/ugcPosts'
    PARAM = {'q':'authors', 'authors':'List(urn%3Ali%3Aorganziation%3A9481327)', 
             'sortBy':'LAST_MODIFIED'
    }
    headers = {'Content-Type': 'application/x-www-form-urlencoded',
               'Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}
    response = requests.get(url=URL, params = PARAM, headers=headers)
    print(response.json())
    
get_comments(access_token)

{'serviceErrorCode': 100, 'message': 'Field Value validation failed in PARAMETER: Data Processing Exception while processing fields [/authors]', 'status': 403} --> how to specify the owner field correctly?

3 Answers

Try using the UCGPost API to get the posts. It is mostly similar to your current request, with a few differences:

  • Base URL is https://api.linkedin.com/v2/ugcPosts
  • URN is encoded, unlike the Shares API
  • viewContext=AUTHOR is the query parameter that needs to passed

There are other requests in the UCGPost API as well that you can try, but the key difference here is to encode the URN.

I had a similar problem and found two solutions - either via ugcPosts-API or shares-API.

ugcPosts-API:

def get_posts(access_token, organisation):
    headers = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization':'Bearer {}'.format(access_token),'X-Restli-Protocol-Version':'2.0.0'}

    url = 'https://api.linkedin.com/v2/ugcPosts?q=authors&authors=List(urn%3Ali%3Aorganization%3A' + str(organisation) + str(")")
    response = requests.get(url=url, headers=headers)
    return response.json()

Shares-API:

def get_shares(access_token, organisation):
    headers = {'Content-Type': 'application/x-www-form-urlencoded','Authorization':'Bearer {}'.format(access_token)}

    URN = 'urn:li:organization:' + str(organisation) + str('&sortBy=LAST_MODIFIED&sharesPerOwner=100')
    url = 'https://api.linkedin.com/v2/shares?q=owners&owners=' + URN
    response = requests.get(url=url, headers=headers)
    return response.json()

... whereas "access_token" is the access token and "organisation" is the ID. (e.g. "2414183")

Important to note is with the latter solution, the "X-Restli-Protocol-Version" information should not be included in the header.

Easy answer since your edit on 30/05: you have a typo in:

urn:li:organziation:9481327

It should be:

urn:li:organization:9481327

As hinted by the error message:

Processing Exception while processing fields [/owners]

The issue is in the owners parameter from your request (at root). And a good look at that field value is enough to find the typo :) (you can compare with the expected URN type from the official documentation.

Related