I've been trying to retrieve Ad Analytics data referring this page
It seems the output from the API is separated into pieces using pagination. So I followed the document explaining how to perform pagination in LinkedIn API. However, I keep experiencing 'Setting a paging start value is not permitted' error. This does not happen when I request the first page but every time I request 'next page', the error encounters.
Here is what I did in 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)
And here is the output from the code above:
# 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}
From my understanding, and as mentioned in the document - To paginate through results, begin with a start value of 0 and a count value of N. To get the next page, set start value to N, while the count value stays the same. Subsequent pages start at 2N, 3N, 4N, and so on., I have to set the start value when passing request for n-th page but somehow it did not work what I intended!
Does anyone have similar experience and can anyone share how to resolve this? Or, please feel free to point me out what I'm doing it wrong in the code.
Thanks!