获取 LinkedIn 公司股票 Python

Getting LinkedIn company shares Python

我正在尝试通过 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 请求获取 Python 中的公司股份。

基于this tutorial, the LinkedIn documentation and 我使用LinkedIn测试组织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)

错误代码为{'serviceErrorCode': 0, 'message': 'Resource shares does not exist', 'status': 404}

使用实际公司 ID (9481327) 时,错误消息保持不变。 The answer to this question does not provide any code or hint for above problem. 基于 V1 api,现已弃用。

更新 30/05/2022 - 下面的函数找到资源,但无法处理参数。

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}
LinkedIn 的测试页面 (2414183) 和我要访问的实际公司页面 (9481327) 的错误消息相同

Up-date 01/06/2022 使用 ugcPost API 提供类似的错误消息

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} --> 如何正确指定owner字段?

自 2005 年 30 月编辑以来的简单回答:您有一个错字:

urn:li:organziation:9481327

应该是:

urn:li:organization:9481327

如错误消息所提示:

Processing Exception while processing fields [/owners]

问题出在您请求的 owners 参数中(根)。仔细查看该字段值就足以找到拼写错误 :)(您可以与 the expected URN type from the official documentation.

进行比较

尝试使用 UCGPost API 获取帖子。它与您当前的请求大体相似,但有一些差异:

  • 基础 URL 是 https://api.linkedin.com/v2/ugcPosts
  • URN 是 编码的 ,与共享 API
  • 不同
  • viewContext=AUTHOR是需要传递的查询参数

UCGPost 中还有其他请求 API 您也可以尝试,但这里的主要区别在于对 URN 进行编码。

我遇到了类似的问题并找到了两个解决方案 - 通过 ugcPosts-API 或 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()

... 而“access_token”是访问令牌,“组织”是 ID。 (例如“2414183”)

需要注意的是后一种解决方案,“X-Restli-Protocol-Version”信息不应包含在 header.