3

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?

Simone
  • 497
  • 5
  • 19
  • @YScharf not sure what you mean? I successfully passed a api.linkedin.com/v2/me request (see post description) – Simone May 30 '22 at 12:29
  • 1
    What type of media are you trying to retrieve? Posts, or comments? – Abhinav Mathur May 31 '22 at 03:51
  • @AbhinavMathur in a first step posts that the company made (later also comments to these posts) I also saw the [ucgPost API](https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api?tabs=http) but encoutering the same problem as with the [shares API](https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/share-api?tabs=http) – Simone Jun 01 '22 at 08:38
  • Have you encoded the URN for ucgPost API? – Abhinav Mathur Jun 01 '22 at 09:21

3 Answers3

2

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.

ai_driana
  • 94
  • 2
  • If V1.0 is deprecated, how would `X-Restli-Protocol-Version` make a difference? – Abhinav Mathur Jun 02 '22 at 09:15
  • 1
    ¯\\_(ツ)_/¯ it just worked when it was not included. Thought to mention it in case somebody would have the idea to use the get_shares-method with the ugc-API - as a heads up that it might not work. – ai_driana Jun 02 '22 at 09:50
  • @AbhinavMathur I read somewhere that X-Restli-Protocol should not be included in the header for a specific API, but should be included for another API. LinkedIn is really touch and go.. – Simone Jun 03 '22 at 10:39
  • Does anyone know how to get the shares + a url to attaches images? – Xen_mar Apr 02 '23 at 18:17
1

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.

Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
  • What would the complete request look like? According to the [docs](https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api?tabs=http#find-ugc-posts-by-authors) ```viewContext=AUTHOR``` needs to be specified for a request for a specific post / URN – Simone Jun 01 '22 at 09:42
  • and yes I encoded the URN :-) – Simone Jun 01 '22 at 09:43
  • @Simone you can use the `https://api.linkedin.com/v2/ugcPosts?ids=List({encoded ugcPostUrn},{encoded ugcPostUrn})` request for multiple URNs – Abhinav Mathur Jun 01 '22 at 09:45
  • Adjusted the PARAM as follows ```PARAM ={'ids':'List(urn%3Ali%3Aorganziation%3A9481327)' }``` gives ```{'message': 'Invalid value type for parameter ids', 'status': 400}``` – Simone Jun 01 '22 at 10:21
  • @Simone for UGCPost, you can't use the organisation URN. Either use the ugcPost URN or shareId URN – Abhinav Mathur Jun 01 '22 at 10:25
  • According to the docs I should be able to. Maybe the docs are wrong? Read something about this. How to get the ugcPost URN? Also I would need to make two requests instea of one. – Simone Jun 01 '22 at 10:31
  • If you read [this](https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/ugc-post-api?tabs=http#get-ugc-posts-by-urn), you'll see the URNs mentioned. The documentation being wrong might be possible, since this is a common issue – Abhinav Mathur Jun 01 '22 at 10:34
  • Yes I see that. But how to get the URNs to retrieve the actual post? I guess back to the shares API.. – Simone Jun 01 '22 at 11:30
  • Got the URN of a specific activity (post etc.) with the solution Adriana posted. Might be useful for somebody else. – Simone Jun 03 '22 at 10:41
0

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.

Guillaume
  • 5,497
  • 3
  • 24
  • 42
  • While I appreciate you pointing out the typo it is not a solution. New error message ```Invalid value type for parameter owners', 'status': 400``` I ran into the error code 400 and others before I created the post...... --> I did not want a quick one line answer with a link to the reference manual. This is already out there but does not solve the problem. See the linked resources in my post. – Simone May 30 '22 at 10:47