I initially get a list of campaign_ids from the LinkedIn account
headers = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Content-Type': 'application/json'
}
campaigns_url = f'https://api.linkedin.com/v2/adCampaignsV2?q=search&search.account.values[0]=urn:li:sponsoredAccount:XYZ1234'
response = requests.get(campaigns_url, headers=headers)
if response.status_code == 200:
campaigns_data = response.json()['elements']
else:
print(f"Error fetching campaigns: {response.status_code}")
print(response.json())
campaigns_data = []
campaign_ids = [campaign['id'] for campaign in campaigns_data]
print(campaign_ids)
I retrieve the list of campaign_ids successfully. Then I want to retrieve the statistics of each sponsored campaign using the campaign ids through the following:
headers_2 = {
'Authorization': f'Bearer {ACCESS_TOKEN}',
'Linkedin-Version': '202409',
'X-Restli-Protocol-Version': '2.0.0'
}
base_statistics_url='https://api.linkedin.com/rest/adAnalytics'
for campaign_id in campaign_ids:
params = {
'q': 'statistics',
'pivots': 'List(CAMPAIGN,CREATIVE)',
'dateRange': '(start:(year:2020,month:1,day:1)',
'timeGranularity': 'YEARLY',
'campaigns': f'List(urn:li:sponsoredCampaign:{campaign_id})'
}
ad_analytics_response = requests.get(base_statistics_url, headers=headers_2, params=params)
print(ad_analytics_response.json()).
However, I get the following response from the server:
{'message': 'Internal Server Error', 'status': 500}
2