How to get all managed accounts with one call?

22 views
Skip to first unread message

cos...@gmail.com

unread,
Mar 2, 2018, 1:23:47 AM3/2/18
to AdWords API Forum
I need to fetch all the accounts a user has access to. I'm using the python library.
At the moment I do something like this, but it seems an overkill and multiple loops / calls.



 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')


Is there a way to simplify this code and just get directly all the ads a user has access to?

Peter Oliquino

unread,
Mar 2, 2018, 2:28:38 AM3/2/18
to AdWords API Forum
Hi,

You can try the GetAccountHierarchy Python example to retrieve all accounts under a specific MCC. Let me know if this is what you are looking for.

Best regards,
Peter
AdWords API Team
Reply all
Reply to author
Forward
0 new messages