Google Ads API & Python - How to get all accounts (name + ID) that a user has access
I'm trying to get the account name and ID of all the Google Ads accounts that the logged user has access to, so the user can later check the stats of each of those accounts.
I've managed to get the IDs using the code on https://developers.google.com/google-ads/api/docs/account-management/listing-accounts?hl=es-419.
But I need the names too and apparently you have to make one API call for each ID to get their account names or any other info.
I've tried with the following Python (Django) code, but it's not working (it probably could be improved a lot and maybe has mistakes, I'm a Python beginner):
def one(request): client = credenciales(request) ga_service = client.get_service("GoogleAdsService") # Get customer resource names from the original code customer_service = client.get_service("CustomerService") accessible_customers = customer_service.list_accessible_customers() customer_resource_names = accessible_customers.resource_names # Prepare a list to store customer data list_clients = [] # Iterate through each customer resource name for resource_name in customer_resource_names: # Extract the customer ID from the resource name custom_id = resource_name.split('/')[-1] # Create a query using the customer_id query = f''' SELECT customer_client.descriptive_name, customer_client.id, customer_client.status FROM customer_client ''' stream = ga_service.search_stream(customer_id=custom_id, query=query) for batch in stream: for row in batch.results: data_clients = {} data_clients["descriptive_name"] = row.customer_client.descriptive_name data_clients["id"] = row.customer_client.id data_clients["status"] = row.customer_client.status list_clients.append(data_clients) # Pass the list of customer data to the template context = { 'list_clients': list_clients, } return render(request, 'one.html', context)
It gives me the error "authorization_error: USER_PERMISSION_DENIED" (which I don't understand, because it should be automatically showing the accounts that the user has permission to access, I'm not giving those accounts/IDs manually).