oauth2_client = oauth2.GoogleRefreshTokenClient(app.config['GOOGLE_CLIENT_ID'], app.config['GOOGLE_CLIENT_SECRET'], creds['refresh_token'])
adwords_client = adwords.AdWordsClient(app.config['ADWORDS_DEV_TOKEN'], oauth2_client)
campaign_service = adwords_client.GetService('CampaignService', version='v201710')
customers = adwords_client.GetService('CustomerService').getCustomers()
for top_customer in customers: # Top Adwords accounts
client_customer_id = str(top_customer.customerId)
top_adwords_client = adwords.AdWordsClient(app.config['ADWORDS_DEV_TOKEN'], oauth2_client, 'user_agent', client_customer_id=client_customer_id)
# Get managed accounts
accounts = top_adwords_client.GetService('ManagedCustomerService').get({ 'fields': ['CustomerId', 'CanManageClients'] })
if 'entries' in accounts:
for account in accounts['entries']:
account_id = account.customerId
adwords_client = adwords.AdWordsClient(app.config['ADWORDS_DEV_TOKEN'], oauth2_client, 'user_agent', client_customer_id=account_id)
campaign_service = adwords_client.GetService('CampaignService', version='v201710')