googleads / googleads-adsense-examples

Samples for the AdSense Management API
Apache License 2.0
144 stars 192 forks source link

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

Open gmehta1996 opened 1 year ago

gmehta1996 commented 1 year ago

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.

muratarchon commented 1 year ago

Hi, I have same error, did you find the solution ?

dugwood commented 1 year ago

@gmehta1996 @muratarchon it seems that you can only use this API using OAuth, not API keys.

Here: https://developers.google.com/adsense/management/reference/rest/v2/accounts/list When you try the API on the right, and only set API keys, you'll have an error:

"message": "API keys are not supported by this API. Expected OAuth2 access token or other authentication credentials that assert a principal. See https://cloud.google.com/docs/authentication",

So we have to use OAuth2, as in the examples: https://github.com/googleads/googleads-adsense-examples/blob/9f1b24bbfdecf418ee48ce18bc3422aef835a138/v2/php/adsense-sample.php#L114C13-L114C38 (PHP).

It's misleading as the client_id and client_secret are used, but not as API keys: it's for OAuth security.