python - 获取 LinkedIn 公司股票 Python

标签 python python-requests linkedin

我正在尝试使用 requests library 获取 Python 中的公司股份.我的应用程序具有额外的营销开发人员平台访问权限,并且我是公司页面的 super 管理员,我正试图从中获取共享。作者described here工作正常。我可以成功放置 https://api.linkedin.com/v2/me获取请求。

基于this tutorial , LinkedIn documentationthis suggestion我使用 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没有为上述问题提供任何代码或提示。 This question基于 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': '参数所有者的值类型无效', 'status': 400}
LinkedIn 的测试页面 (2414183) 和我要访问的实际公司页面 (9481327) 的错误消息相同

更新 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': 'PARAMETER 中的字段值验证失败:处理字段时出现数据处理异常 [/authors]', 'status': 403} -->如何正确指定所有者字段?

最佳答案

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

共享 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”是访问 token ,“organisation”是 ID。 (例如“2414183”)

需要注意的是后一种解决方案,标题中不应包含“X-ReSTLi-Protocol-Version”信息。

关于python - 获取 LinkedIn 公司股票 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72406550/

相关文章:

OAUTH - 从 LinkedIn 请求经过验证的电子邮件地址

python - python 的 'pygame.key.get_pressed()' 模块无法识别按键

python - 无法从 'StandardScalar' 导入名称 'sklearn.preprocessing'

c++ - 导入 ctype;在 C++ 应用程序中嵌入 python

python - 如何创建自定义 crontab?

python - 如何实现这个python post并获取?

python-requests - 查询字符串中的字典类型参数

python - 如何使请求不解码我的网址中的转义百分比?

Linkedin Oauth2 客户端凭据授予类型

api - 如何通过 API 将照片/图像上传到 LinkedIn?