LinkedIn adAnalytics API - 按公司方面匹配时出现 http 500 错误

问题描述 投票:0回答:1

我正在使用 LinkedIn 广告报告 API 来检索受众列表中每个公司的展示次数、目标会员和参与度数据。

我能够按

MEMBER_COMPANY
进行旋转并轻松获取除
approximateMemberReach
指标之外的所有数据。该文档明确指出:“仅限非人口统计数据(即不是 MEMBER_)。”。

我试图规避这个问题的方法是首先检索公司列表,然后为列表中的每个公司调用一次 API,以匹配公司 ID。

但是,当我尝试应用

companies
方面时,我得到 HTTP 500。


fields = [
  "approximateMemberReach",
  "impressions",
  "pivotValues"
]

url = f"https://api.linkedin.com/rest/adAnalytics?q=analytics&companies=List(urn%3Ali%3Aorganization%3A{company_id})&pivot=CAMPAIGN&timeGranularity=ALL&dateRange=(start:(day:{start_day},month:{start_month},year:{start_year}),end:(day:{end_day},month:{end_month},year:{end_year}))&accounts=List(urn%3Ali%3AsponsoredAccount%3A{account_id})&fields={','.join(fields)}"

headers = {
  'LinkedIn-Version': '202405',
  'X-Restli-Protocol-Version': '2.0.0',
  'Authorization': f"Bearer {token}",
}

response = requests.request("GET", url, headers=headers)

print(response.text)

如果我删除上面的

companies=List(org URN)
参数,它会按预期工作。 HTTP 响应没有提供任何关于错误的信息,它只是返回 500 服务器错误。

有什么想法吗?

linkedin-api
1个回答
0
投票

我将从一些关键更改开始,例如:使用 urllib.parse.quote 确保 company_id 的正确 URL 编码,简化请求结构以逐步添加参数并识别问题,并包括调试步骤以在以下情况下打印状态代码和响应详细信息出现500错误。这将有助于隔离导致错误的请求的特定部分。

请检查参数是否正确

import requests
import urllib.parse

# Define parameters
company_id = "123456"
account_id = "7891011"
start_day = 1
start_month = 1
start_year = 2023
end_day = 31
end_month = 12
end_year = 2023
token = "YOUR_ACCESS_TOKEN"

fields = [
    "approximateMemberReach",
    "impressions",
    "pivotValues"
]

# URL encode the company ID
encoded_company_id = urllib.parse.quote(f"urn:li:organization:{company_id}")

url = (
    f"https://api.linkedin.com/rest/adAnalytics?q=analytics"
    f"&companies=List({encoded_company_id})"
    f"&pivot=CAMPAIGN"
    f"&timeGranularity=ALL"
    f"&dateRange=(start:(day:{start_day},month:{start_month},year:{start_year}),end:(day:{end_day},month:{end_month},year:{end_year}))"
    f"&accounts=List(urn%3Ali%3AsponsoredAccount%3A{account_id})"
    f"&fields={','.join(fields)}"
)

headers = {
    'LinkedIn-Version': '202405',
    'X-Restli-Protocol-Version': '2.0.0',
    'Authorization': f"Bearer {token}",
}

# Make the request and print the response
response = requests.get(url, headers=headers)
print(response.status_code)
print(response.text)

# If HTTP 500, print more details for debugging
if response.status_code == 500:
    print("Server Error: 500")
    print("Response Headers:", response.headers)
    print("Response Text:", response.text)
© www.soinside.com 2019 - 2024. All rights reserved.