You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用LinkedIn API获取指定UGC帖子统计数据报参数不允许错误

问题排查结论

根因分析

  • LinkedIn v2 API遵循RestLi 2.0协议规范,数组类型参数不支持参数名[下标]=值的普通表单传参格式,你当前请求URL里的ugcPosts[0]=xxx&ugcPosts[1]=xxx写法不符合协议要求,才会触发Unpermitted fields present in PARAMETER的403错误。
  • 你已配置的权限scope、请求头规则都符合接口要求,无需调整。

修复方案

把ugcPosts参数的传参格式改为ugcPosts=List(元素1,元素2),多个URN用英文逗号分隔即可,不需要加下标。

修改后的完整代码示例

import requests
import json
 
from liapiauth import auth, headers

def organization_info(headers):
    # 改用params参数传参,避免手动拼接URL出现编码错误
    params = {
        "q": "organizationalEntity",
        "organizationalEntity": "urn:li:organization:00000",
        "ugcPosts": "List(urn:li:ugcPost:1111111,urn:li:ugcPost:2222222)"
    }
    response = requests.get(
        'https://api.linkedin.com/v2/organizationalEntityShareStatistics',
        params=params,
        headers=headers
    )
    # 增加响应状态码判断,方便快速定位其他异常
    if response.status_code == 200:
        return response.json()
    else:
        print(f"请求失败,状态码:{response.status_code},错误信息:{response.text}")
        return None
 
if __name__ == '__main__':
    credentials = 'credentials.json'
    access_token = auth(credentials)
    headers = headers(access_token)
    organization_info = organization_info(headers)
    if organization_info:
        with open('lisharelist.json', 'w', encoding='utf-8') as outfile:
            json.dump(organization_info, outfile, ensure_ascii=False, indent=2)

额外优化建议

  • 所有请求参数都通过requests的params参数传递,不要手动拼接URL,requests会自动处理URL编码,避免特殊字符转义错误。
  • 导出json文件时添加ensure_ascii=False配置,避免中文被自动转义为Unicode编码。

内容的提问来源于stack exchange,提问作者Piotr Chmurzynski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 05:27:04