How do you use the Python Adsense Management API to create adunits?
Based on documentation and some examples I've found, it should be something like:
#!/usr/bin/env python
"""
pip install google-auth google-auth-oauthlib google-auth-httplib2
"""
from google.oauth2 import service_account
from googleapiclient.discovery import build
account_id = 'pub-<id>'
ad_client_id = 'ca-pub-<id>'
# Load credentials from the downloaded JSON file
credentials = service_account.Credentials.from_service_account_file('myservicekey.json', scopes=['https://www.googleapis.com/auth/adsense'])
# Create AdSense API service
adsense_service = build('adsense', 'v2', credentials=credentials)
# Define the ad client data
ad_client_data = {
"productCode": "AFC",
"supportsReporting": True,
"kind": "adsense#adClient",
}
# Define the ad unit data
ad_unit_data = {
"contentAdsSettings": {
"backupOption": {
"type": "USE_DEFAULT"
},
"size": "SIZE_UNSPECIFIED"
},
"name": "Test Ad 123",
"customStyle": {
"colors": {
"border": "#ffffff",
"title": "#333333",
"background": "#ffffff",
"text": "#666666",
"url": "#1a0dab"
}
},
"mobileContentAdsSettings": {
"size": "SIZE_UNSPECIFIED"
},
"settings": {
"size": ["SIZE_UNSPECIFIED"]
}
}
# Create the ad unit, none of these work.
# ad_unit = adsense_service.accounts().adunits().create(body=ad_unit_data, parent=ad_client['name']).execute()
# ad_unit = adsense_service.accounts().adunits().create(body=ad_unit_data, parent=f'accounts/{account_id}/adclients/{ad_client_id}').execute()
# ad_unit = adsense_service.accounts().adclients().adunits().create(body=ad_unit_data, parent=f'accounts/{account_id}/adclients/{ad_client_id}').execute()
# ad_unit = adsense_service.accounts().adunits().insert(body=ad_unit_data, accountId=account_id, adClientId=ad_client_id).execute()
ad_unit = adsense_service.accounts().adclients().adunits().create(body=ad_unit_data, parent=f'accounts/{account_id}/adclients/{ad_client_id}').execute()
However, all variations of the final call to create ad_unit fail with the error AttributeError: 'Resource' object has no attribute 'create' or AttributeError: 'Resource' object has no attribute 'insert' if I try to use the older insert() method.
My call appears to correctly follow the accounts.adclients.adunits.create path listed in the v2 docs.
What am I doing wrong? Why is the create() method missing?