googleads-adsense-examples icon indicating copy to clipboard operation
googleads-adsense-examples copied to clipboard

Adsense API returning Error 403 - "The caller does not have permission"

Open gmehta1996 opened this issue 1 year ago • 2 comments

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

{ "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED" } }

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.

gmehta1996 avatar Mar 17 '23 11:03 gmehta1996