Posts tagged with service-accounts

I´d like to use the Google Ads API to programmatically retrieve ads data from my Google Ads account. I`d like to use a PHP script (Laravel command and the Google Ads API PHP client library) triggered by a cron-job to retrieve this data regularly. After reading the documentations a couple of times I still have a few questions:

Q1: My understanding is that for such "program to program" API access with no user involvement, I need to follow the service account authentication scenario. Is that right?

Q2: The service account documentation says that I have to have Google Workspace Domain with my company domain, email address, etc. My company website, email is hosted by another provider, do I really need to pay for the Google Workspace Domain, just to be able use the Google Ads API? Any alternative solutions?

Thanks for any suggestions on how to do this right. W.

I'm trying to use Google Ads API using service account.
The following works:

from google.ads.googleads.client import GoogleAdsClient from google.oauth2.service_account import Credentials as ServiceAccountCreds oauth2_client = ServiceAccountCreds.from_service_account_file(     filename=key_file, subject=developer_account,     scopes=["https://www.googleapis.com/auth/adwords"] ) google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=oauth2_client,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_serice.search_stream(customer_id=self.client_customer_id, query =  query) 

I want to to do following with Identity Workload Federation(w/o key):

from google.auth import aws as google_auth_aws from google.ads.googleads.client import GoogleAdsClient credentials = google_auth_aws.Credentials.from_info(json_config_info)  # from workload identity federation credentials = credentials.with_scopes(["https://www.googleapis.com/auth/adwords"]) credentials = credentials.with_subject(self.developer_account) # Build the service object. self.google_ads_client = GoogleAdsClient(login_customer_id=self.login_customer_id, credentials=credentials,                                     developer_token=self.developer_token) self.ga_service = self.google_ads_client.get_service("GoogleAdsService", version="v9") self.ga_service.search_stream(customer_id =self.client_customer_id, query =  self.query) 

Which throws error: ERROR:'Credentials' object has no attribute 'with_subject'.

I want to modify these API calls to use a GCP service account instead of my identity.

var report = AdsApp.search(searchQuery);    var table = BigQuery.Tables.insert(table, projectId, dataSetId); BigQuery.Jobs.insert(job, BIGQUERY_PROJECT_ID); 

I read this article but wasn't sure how to find the url for AdsApp and BigQuery.

I searched this post, but still haven't been sure.

Hi,

I want to write an Ads-script to call GoogleAds url nad BQ url using a service account.

I saw this code. Are my urls to the GAds and BQ api correct?

Thanks!

=====================

  // Natural Language API Sentiment URL   var url =       'https://language.googleapis.com/v1beta1/documents:analyzeEntities';   var options = {     method: 'POST',     contentType: 'application/json',     payload: JSON.stringify(body)   };   var response = authUrlFetchApp.fetch(url, options);   var result = JSON.parse(response.getContentText());   Logger.log(result);   if (result.entities) {     // return a list of identified entities     return result.entities;   }   throw Error('No entities response returned'); } 

Im building an app with google ads api using a service account auth flow (server to server).

The problem: the auth part is not working... I keep getting a 401 error.

Sample of the request:

const request = require('request'); .... request({   'method': 'GET',   'url': 'https://googleads.googleapis.com/v6/customers/XXXXXXXX',   'headers': {     'Authorization': 'Bearer XXXXXXXX',     'developer-token': 'XXXXXXXX',     'Content-Type': 'application/json'   } }) ... 

or

curl --location --request GET 'https://googleads.googleapis.com/v6/customers/XXXXXXXX' \ --header 'Authorization: Bearer XXXXXXXX' \ --header 'developer-token: XXXXXXXX' \ --header 'Content-Type: application/json' 

[ERROR] Response from google ads api:

{         "code": 401,         "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",         "status": "UNAUTHENTICATED"     } 

I cant seem to find out why Im getting this authentication error.

What I have tried:

  1. Following this guide step by step -> https://developers.google.com/google-ads/api/docs/oauth/service-accounts
  2. Using the python library -> https://github.com/googleads/googleads-python-lib
  3. Using the google ads API with REST (no library)
  4. On localhost server
  5. On the production server (the www.g-suite.com domain linked to the account)

Whatever method or environment I try, it results in the same error.

Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential 

Steps I have implemented:

  • Created a google ads manager account
  • Generated a valid developer token for that account
  • Created project in Google cloud platform
  • Opened a service account inside the project
  • Created a private key for the service account
  • Granting impersonation abilities in the G Suite domain for this scope: https://www.googleapis.com/auth/adwords
  • Enabled domain-wide delegation on the service account
  • Generated the access token with the key json file.

Generating the access token:

const { google } = require('googleapis'); const getAccessToken = async () => {     const SCOPES = ['https://www.googleapis.com/auth/adwords'];     const authClient = new google.auth.GoogleAuth({         keyFile: './pathtokeyfile.json',         scopes: SCOPES,     });     const token = await authClient.getAccessToken();     return token; }; 

I am surely missing something, but am not sure what it is..

Will be super grateful if someone can share a solution!! thanks champs 👍