Hi I'm trying to fetch the ad unit details of an Adsense Account using the Adsense Management API. I created the application on google cloud console and got the client_id and client_secret which I used to get the refresh token as well.
I used the following code snippet to get account id and name:
service = discovery.build('adsense', 'v2', credentials = credentials)
# Retrieve account list in pages and display data as we receive it.
request = service.accounts().list(pageSize=50)
# while request is not None:
result = request.execute()
print(result)
accounts = result['accounts']
for account in accounts:
print ('Account with ID "%s" and name "%s" was found. '
% (account['name'], account['displayName']))
And I was able to get the account name and Id using this , then I further tried to get the ad unit details but I'm getting the mentioned error:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://adsense.googleapis.com/v2/accounts/{account_id}/adclients/{adclient_id}/adunits?pageSize=50&alt=json returned "The caller does not have permission". Details: "The caller does not have permission">
The code snippet I'm using here is:
request = service.accounts().adclients().adunits().list(
parent=f"accounts/{accountId}/adclients/{adClientId}", pageSize=50)
result = request.execute()
Here I'm using the publisher id of the account in the variable - accountId and the customer id in the variable - adClientId.
Am I using the correct IDs? if not from where do I find the ids. If the ids being used are correct then I'm getting this permission error stated above
I have added these scopes as well
https://www.googleapis.com/auth/adsense
https://www.googleapis.com/auth/adsense.readonly
Also I tried the same in the APIs Explorer UI and got the same result
What permission do I require here or what can be the solution to get the ad units details for Adsense. I would really appreciate the help as I'm stuck since many days.
I was able to resolve the issue myself. The ad client id was not correct So I used the Adsense service accounts.adclients.list and got the list of ad client ids (https://developers.google.com/adsense/management/reference/rest/v2/accounts.adclients/list ) From there I tried all ids available and used the same service stated above and was able to get the ad unit details