我一直在尝试引用this page
检索广告分析数据似乎使用分页将API的输出分为几部分。 因此,我遵循the document解释了如何在LinkedIn API中执行分页。但是,我一直遇到 “不允许设置分页开始值” 错误。当我请求第一页时不会发生这种情况,但是每次我请求“下一页”时都会遇到错误。
这是我在Python中所做的:
import requests
# Define token, base_url, and parameters
headers = {'Authorization':'Bearer ' + 'MY_TOKEN'}
base_url = 'https://api.linkedin.com/v2/adAnalyticsV2/'
params = {
'q': 'analytics',
'dateRange.start.month': '4',
'dateRange.start.day': '1',
'dateRange.start.year': '2019',
'timeGranularity': 'DAILY',
'pivot': 'CREATIVE',
'accounts':'MY_ACCOUNT',
'campaignGroups': 'MY_CAMPAIGN_GROUP',
'count':100,
}
# Get analytics data by paginating
url_stack = [base_url]
while url_stack:
# Pop the url in the list, now the list is empty
wurl = url_stack.pop()
# Make request to API
r = requests.get(wurl, params=params, headers=headers)
if r.ok:
print('Request success')
print('URL of successful request:\n- {}\n'.format(r.url))
else:
print('Request fail')
print('URL of failed request:\n- {}'.format(r.url))
print('Request detail:\n- {}\n'.format(r.json()))
# If paging information exists in the response,
if 'paging' in r.json().keys():
# Use the inforamtion for the next page by updating 'params' dict
params['start'] = r.json()['paging']['start'] + r.json()['paging']['count']
params['count'] = r.json()['paging']['count']
# Append updated URL with next paging info
url_stack.append(wurl)
这是上面代码的输出:
# Output:
Request success
URL of successful request:
- https://api.linkedin.com/v2/adAnalyticsV2/?q=analytics&dateRange.start.month=4&dateRange.start.day=1&dateRange.start.year=2019&timeGranularity=DAILY&pivot=CREATIVE&accounts=MY_ACCOUNT&campaignGroups=MY_CAMPAIGN_GROUP&count=100
Request fail
URL of failed request:
- https://api.linkedin.com/v2/adAnalyticsV2/?q=analytics&dateRange.start.month=4&dateRange.start.day=1&dateRange.start.year=2019&timeGranularity=DAILY&pivot=CREATIVE&accounts=MY_ACCOUNT&campaignGroups=MY_CAMPAIGN_GROUP&count=100&start=100
Request detail:
- {'serviceErrorCode': 2, 'message': 'Setting a paging start value is not permitted', 'status': 400}
据我所知,并且如文档中所述- 要对结果进行分页,请以0的起始值和N的计数值开头。要获得下一页,请将起始值设置为N ,而计数值保持不变。随后的页面从2N,3N,4N等开始。 ,在传递第n个页面的请求时,我必须设置起始值,但不知何故,它无法达到我的预期! >
有人有类似的经历吗,谁能分享解决方法? 或者,请随时指出我在代码中做错了什么。
谢谢!