"Parameter 'dateRange' is required", when 'dateRange' is provided, what format should it be?
Question:
I’m trying to get some data from the following API https://api.linkedin.com/v2/adAnalyticsV2?q=analytics
.
Here is the code I’m using to do it
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2?q=analytics'
headers = {
'Authorization': f'Bearer {token}',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0',
'pivot': 'ACCOUNT',
'dateRange': json.dumps({
'start': {
'month': 5,
'year': 2019,
'day': 28
},
'end': {
'month': 5,
'year': 2022,
'day': 28
}
}),
'timeGranularity': 'DAILY'
}
response = requests.get(url, headers=headers).json()
print(response)
return response
And I get a response:
{‘message’: "Parameter ‘dateRange’ is required", ‘status’: 400}
What should be the format of dateRange?
I tried different formats:
'dateRange': '{start = 1546300800000, end = 1609459200000}',
'dateRange': '(start:(day:1,month:1,year:2022))',
'dateRange': '(start:(day:1,month:1,year:2022))',
I am trying to get a non error response.
Answers:
First, it is not the format which is incorrect. You are trying to include the 'dateRange'
parameter as a header, but the LinkedIn API documentation specifies that the 'dateRange'
parameter should be included as a query string parameter in the URL.
To include the 'dateRange'
parameter in the query string of the URL, you can use the params
parameter of the requests.get()
function.
Second, you should leave out the json.dumps()
call. The following code shouldn’t lead to an error:
import requests
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2?q=analytics'
params = {
'q': 'analytics',
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
'timeGranularity': 'DAILY',
'pivot': 'COMPANY'
}
headers = {
'Authorization': f'Bearer {token}',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0'
}
response = requests.get(url, params=params, headers=headers).json()
print(response)
return response
Ok,
so I was finally able to go further. The working code is:
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2'
headers = { 'Authorization': f'Bearer {token}'}
params = {
'q': 'analytics',
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
'pivot': 'COMPANY',
'timeGranularity': 'DAILY'
}
response = requests.get(url, headers=headers, params=params)
return response
The working format for dateRange is:
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
Now the api is returning the error:
"{‘serviceErrorCode’: 3, ‘message’: ‘Missing facet’, ‘status’: 400}"
But this is connected with adding some more parameters which, by the way, are not so easy to understand from the documentation (https://learn.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/ads-reporting?view=li-lms-unversioned&tabs=curl#metrics-available-1)
I’m trying to get some data from the following API https://api.linkedin.com/v2/adAnalyticsV2?q=analytics
.
Here is the code I’m using to do it
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2?q=analytics'
headers = {
'Authorization': f'Bearer {token}',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0',
'pivot': 'ACCOUNT',
'dateRange': json.dumps({
'start': {
'month': 5,
'year': 2019,
'day': 28
},
'end': {
'month': 5,
'year': 2022,
'day': 28
}
}),
'timeGranularity': 'DAILY'
}
response = requests.get(url, headers=headers).json()
print(response)
return response
And I get a response:
{‘message’: "Parameter ‘dateRange’ is required", ‘status’: 400}
What should be the format of dateRange?
I tried different formats:
'dateRange': '{start = 1546300800000, end = 1609459200000}',
'dateRange': '(start:(day:1,month:1,year:2022))',
'dateRange': '(start:(day:1,month:1,year:2022))',
I am trying to get a non error response.
First, it is not the format which is incorrect. You are trying to include the 'dateRange'
parameter as a header, but the LinkedIn API documentation specifies that the 'dateRange'
parameter should be included as a query string parameter in the URL.
To include the 'dateRange'
parameter in the query string of the URL, you can use the params
parameter of the requests.get()
function.
Second, you should leave out the json.dumps()
call. The following code shouldn’t lead to an error:
import requests
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2?q=analytics'
params = {
'q': 'analytics',
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
'timeGranularity': 'DAILY',
'pivot': 'COMPANY'
}
headers = {
'Authorization': f'Bearer {token}',
'cache-control': 'no-cache',
'X-Restli-Protocol-Version': '2.0.0'
}
response = requests.get(url, params=params, headers=headers).json()
print(response)
return response
Ok,
so I was finally able to go further. The working code is:
def get_ad_analytics_v2(token: str) -> str:
url = 'https://api.linkedin.com/v2/adAnalyticsV2'
headers = { 'Authorization': f'Bearer {token}'}
params = {
'q': 'analytics',
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
'pivot': 'COMPANY',
'timeGranularity': 'DAILY'
}
response = requests.get(url, headers=headers, params=params)
return response
The working format for dateRange is:
'dateRange.start.day': '1',
'dateRange.start.month': '1',
'dateRange.start.year': '2022',
Now the api is returning the error:
"{‘serviceErrorCode’: 3, ‘message’: ‘Missing facet’, ‘status’: 400}"
But this is connected with adding some more parameters which, by the way, are not so easy to understand from the documentation (https://learn.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/ads-reporting?view=li-lms-unversioned&tabs=curl#metrics-available-1)