google-ads-python icon indicating copy to clipboard operation
google-ads-python copied to clipboard

Unable to access a customer account for Ads data even with MCC access

Open CodesOfAnurag opened this issue 3 years ago • 3 comments

I Have MCC access under which there are multiple accounts, however, I can only access 3 accounts out of them.

This is the structure of my YAML file.

Screenshot 2022-08-10 at 12 07 46 PM

This is the Error Log for the same.

gads_log.txt

Here is the snippet of the python code used

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
import pandas as pd

from google.protobuf.json_format import MessageToJson
import json

import logging
logging.basicConfig(level=logging.INFO, format='[%(asctime)s - %(levelname)s] %(message).5000s')
logging.getLogger('google.ads.googleads.client').setLevel(logging.INFO)

client = GoogleAdsClient.load_from_storage("/Users/abhishekkumar/Downloads/google_ads_2.yml")
ga_service = client.get_service("GoogleAdsService")

customer_id = "6376354833"

query = f"""
SELECT 
  campaign.id, 
  campaign.name, 
  campaign.status, 
  metrics.cost_micros,
  metrics.ctr, 
  metrics.clicks,
  metrics.all_conversions, 
  metrics.absolute_top_impression_percentage, 
  metrics.impressions, 
  metrics.search_impression_share, 
  campaign.start_date,
  segments.date
FROM campaign 
WHERE 
  segments.date DURING LAST_7_DAYS 
"""

# Issues a search request using streaming.
response = ga_service.search(customer_id=customer_id, query=query)

rows = []

for row in response:  
    serialized_json = MessageToJson(row).replace('\n', '')    
    serialized_json_dict = json.loads(serialized_json)
    
    df_row = pd.json_normalize(serialized_json_dict)  
    rows.append(df_row)
    if len(rows) == 0:
        continue


df = pd.concat(rows)

CodesOfAnurag avatar Aug 10 '22 06:08 CodesOfAnurag

As we discussed in the support forum, the issue here is that the login-customer-id isn't being loaded from yaml, and is thus not being included in the request metadata.

You've already checked:

  1. That the load_from_storage method is loading the yaml file you expect.
  2. You're not modifying the login_customer_id property on the GoogleAdsClient instance.

I'll try testing with a yaml that's structured like yours to see if there's an issue in the config module.

BenRKarl avatar Aug 10 '22 15:08 BenRKarl

@CodesOfAnurag one other thought I had - can you confirm that the other necessary tokens are loaded onto the client instance? If you add the below lines to your script, you should be able to confirm that they match what's in your yaml file. And please don't post any of these token values here, just confirm directly that they match.

print(client.developer_token)
print(client.credentials.client_id)
print(client.credentials.client_secret)
print(client.credentials.refresh_token)

BenRKarl avatar Aug 15 '22 14:08 BenRKarl

@BenRKarl I have added these print statements in my code, the values printed matches the yaml file Screenshot 2022-08-16 at 6 16 05 PM ,

CodesOfAnurag avatar Aug 16 '22 12:08 CodesOfAnurag

@BenRKarl I am not sure what happened exactly over here, I created a new app from scratch on Google Cloud Console to get the new set of credentials in YAML file. I am able to fetch data now.

CodesOfAnurag avatar Aug 17 '22 05:08 CodesOfAnurag