[Google Ads API v6] Fetching data through automated Python script

635 views
Skip to first unread message

Davide Caruso

unread,
Feb 25, 2021, 10:49:48 AM2/25/21
to AdWords API and Google Ads API Forum
I'm having some problems creating a script that automatically retrieves some metrics from Google Ads for a daily report for the company I work in.
I've been reading tens of pages of documentation, troubleshooting and help, but unfortunately they only increased my confusion.
Initially, we only had a Google Ads account, that we use to manage ad campaigns. This is what I did next:
  1. I created an Ad Manager account, and successfully associated it to our Google Ads account, in order to have access to the Google Ads API.
  2. I requested access to the API and, long story short, I now have access at the basic level.
  3. After reading tens of help pages, I decided to use a server-to-server flow. The script I'm creating has to be executed automatically by cronjob, so if I understood correctly I can't use any other authentication method.
  4. I created a new project from the developer console and associated Google Ads API to it.
  5. I created a service account and gave it viewer rights. I didn't enable domain-wide delegation because it requires a G Suite/Google Workspace account, which we don't have, and one of the guides said it's optional.
  6. I generated a JSON key file fr the service account, and saved it on my computer.
  7. I tried a query to test the access. This is a simplified version of the Python 3 code:
from google.ads.google_ads.client import GoogleAdsClient
credentials = {
    'developer_token': ... # received in (2)
    'login_customer_id': ... # the manager account ID
    'json_key_file_path': ... # where I saved the JSON key file from (6)
    impersonated_email': ... # the service account's email
}
client = GoogleAdsClient.load_from_dict(credentials)
ga_service = client.get_service('GoogleAdsService', version='v6')
response = ga_service.search(
    GA_CUSTOMER_ID,
    query='SELECT campaign.cost_micros FROM campaign'
)
for item in response:
    print(item)
(I really hope the code is formatted properly, sorry if it isn't)
The last two lines are for me to get a feeling of what I'm working with, while the rest should be definitive, unless there is a better way, of course.
Instead of getting the response, I get the error NOT_ADS_USER: "User in the cookie is not a valid Ads user."
I obviously did something wrong somewhere, but after three days I really don't know what else to try. The amount of options available is discouraging, and I have the sensation that sometimes the documentation contradicts itself on different pages, or maybe I just misunderstood something.

If anyone could give me some insight, I would be very grateful.
Thanks for having the patience to read this wall of text.

Best Regards
Davide

Google Ads API Forum Advisor Prod

unread,
Feb 26, 2021, 3:20:04 PM2/26/21
to davide.ca...@gmail.com, adwor...@googlegroups.com

Hi Davide,

Thank you for reaching out. The NOT_ADS_USER error usually occurs when the login used to generate the access token is not associated with any Google Ads account. I would suggest you to use the Web Application Flow or the Desktop Application Flow instead as they are less complicated in terms of configuration. Please follow the instructions in this guide to obtain the OAuth2 credentials and try it again.

Thanks and regards,
Xiaoming, Google Ads API Team
 


Google Logo
Xiaoming
Google Ads API Team
 


ref:_00D1U1174p._5004Q2DIxeA:ref

Davide Caruso

unread,
Mar 1, 2021, 3:22:05 AM3/1/21
to Google Ads API Forum Advisor Prod, adwor...@googlegroups.com
Thank you, I tried this and it seems to work.

Davide Caruso

unread,
Mar 8, 2021, 5:22:32 AM3/8/21
to AdWords API and Google Ads API Forum
I was correct, it "seems" to work. The refresh token timed out during the weekend, and I had to create a new one. And while now the script works again, I can't create a new token every week. I tried registering the app, but the requirements are absurd. My script runs exclusively on a server, it's not an app for everyone to use, as the registration process seems to assume. Following it is just an enormous waste of time for me and my colleagues. So I'm back to start. I've been reading manuals all morning now, and I still can't find out how to configure a service account, and if there's something wrong in my code. I'll appreciate any insights. Alternative long-term solutions are also welcome, of course, as long as they don't require some periodic refresh on my side.

Best regards
Davide

Google Ads API Forum Advisor Prod

unread,
Mar 8, 2021, 9:43:04 PM3/8/21
to davide.ca...@gmail.com, adwor...@googlegroups.com
Hi Davide,

I work with Xiaoming and allow me to assist you.

We understand the inconvenience that you are encountering. However, as stated in this guide and in the Key Point section, a non-expiring refresh token should be maintained when using the desktop app flow instead. Otherwise, when using service accounts, you may need to create a refresh token once it expires.

Best regards,

Google Logo
Peter Laurence Napa Oliquino
Google Ads API Team
 


ref:_00D1U1174p._5004Q2DIxeA:ref

Davide Caruso

unread,
Mar 9, 2021, 3:38:33 AM3/9/21
to AdWords API and Google Ads API Forum
Thank you for yur answer, Peter.
I already followed that guide, it brought me to the current situation. I'm not 100% sure of it, but it seems that the non-expiring token can be obtained only if the app is in the production status, while mine is in the testing status. Is that correct?
If it is, switching to production would mean creating a dedicated privacy policy page, a video, and possibly something else. This will require time, likely some days of work for my company, for something nobody will ever need.
If it isn't, then I probably did something wrong previously, and I will try again from scratch.
At the moment the script is working, but if the refresh token expires next week, it means I will have to dedicate every week some time to this task, and I can't have the company depend on me in this way. It's wrong for them, and it's wrong for me.

Best
Davide

Google Ads API Forum Advisor Prod

unread,
Mar 9, 2021, 9:41:12 PM3/9/21
to davide.ca...@gmail.com, adwor...@googlegroups.com
Hi Davide,

Thank you once more for the additional details regarding your issue.

Since the concern is now more related to the behavior or expiration of your service account credentials, then I would recommend that you reach out to the Google API Console support team instead, via this link. This is because their team is the more appropriate to provide guidance as this should be more in line with their expertise.

You may confirm with them if the testing / production nature of your service account indeed affects the validity of your credentials. I hope this helps.
Reply all
Reply to author
Forward
0 new messages