Authenticate Adsense API using python

166 views
Skip to first unread message

Kulvinder Tuteja

unread,
Mar 7, 2023, 12:48:03 PM3/7/23
to AdSense API Forum
Hi,
I'm trying to create an api script with python for Adsense.
I have already created the application with the scopes and using the client_id and client_secret I have also generated the refresh token. Is there a way to initialise a client and service for adsense like it is there in the admanager API?

For instance, I'm using the following code snippet in python to create oauth using client_d, client_secret and refresh_token

try:
oauth2_client = oauth2.GoogleRefreshTokenClient(
client_id, client_secret, refresh_token)
except Exception as err:
return f"Google Refresh Token Authentication Error: {str(err)}"

Then I use the following snippet to initialise the client and use a service

# Initialize AdManager Client.
client = ad_manager.AdManagerClient(oauth2_client, "DFP API", network_code)
# Initialize a service.
report_service = client.GetService('ReportService')

Is there a similar way for Adsense API to initialise a client using client_id, client_secret and refresh token and then use a service?


Ad Sense API

unread,
Mar 7, 2023, 12:52:12 PM3/7/23
to ascendeu...@gmail.com, adsen...@googlegroups.com
Hi Kulvinder,

Thanks for your email. There is a selection of sample code available at https://developers.google.com/adsense/management/libraries, including python examples. Hopefully something there will help.

Regards,
Andrew (AdSense API team)

ref:_00D1U1174p._5004Q2jWz3m:ref

Kulvinder Tuteja

unread,
Mar 17, 2023, 10:41:37 AM3/17/23
to AdSense API Forum
HI Adsense Team,
Thank you I was able to create the script but I get the following error when trying to get adunits details:

"The caller does not have permission". Details: "The caller does not have permission"

service = discovery.build('adsense', 'v2', credentials = credentials)

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']))
# Retrieve the ad units for the provided ad client ID.
request = service.accounts().adclients().adunits().list(
parent=f"accounts/{accountId}/adclients/{adClientId}", pageSize=100)
print(request)

result = request.execute()

Here I'm using the publisher id with the variable accountId and customer id with the variable adClientId
but receiving "The caller does not have permission" error although the other service to get account details is working for me. I have the client_id, client_secret and refresh_token with me and able to use the other service i.e service.accounts().list(pageSize=50)
For the ad unit details I also tried the APIs Explorer but getting the same error there as well:

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

What permissions do I need to check here which are being denied. Kindly help

Screenshot 2023-03-17 at 1.45.34 PM.png

Ad Sense API

unread,
Mar 17, 2023, 10:46:32 AM3/17/23
to ascendeu...@gmail.com, adsen...@googlegroups.com
Hi Kulvinder,

Great that you have made progress. I think the missing part is that you need to call service.accounts().adclients().list() with the parameter "accounts/{accountId}". Then you will have the correct value for the adclient to use as the parent for the call to service.accounts().adclients().adunits().list().

If you still hit problems, I would recommend experimenting with the API explorer (the "Try this method" box) on the management API documentation page at https://developers.google.com/adsense/management/reference/rest/v2/accounts/list
Reply all
Reply to author
Forward
0 new messages