Unable to connect to Google Ads API with service account and the Python librairy
For a week now I have been crawling the web but haven't find any legit documentation to connect the python librairie for Ads API with Service Account.
I want to create a micro-service, server-to-server, so anything else apart service account as authentication is off-table.
On GCP, I was using oAuth2 but I had to manually refresh the tokens with https://developers.google.com/oauthplayground/ which isn't a viable solution.
What I have done so far :
- I created a service account
- generated a key to it in JSON and downloaded it.
- Add all possible access to my service-account permissions, such as :
- Owner
- Security Reviewer
- Service Account Admin
- Service Account Key Admin
- Service Account Token Creator
- Service Account User
4 - Created my script to authenticate :
from google.ads.googleads.client import GoogleAdsClient credentials = { "developer_token": "xXxxxXxxxXxxXXXXxXxxX", "use_proto_plus": true, "json_key_file_path": "./path_to_json.json" , "impersonated_email": "gcp_and_ads_admin_email", "login_customer_id": "XXXXXXXXXX" } client = GoogleAdsClient.load_from_dict(credentials)
When I execute my script, this error follows :
google.auth.exceptions.RefreshError: ('unauthorized_client: Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.', {'error': 'unauthorized_client', 'error_description': 'Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.'})
Does anyone know how I can find a tutorial or help me with this please ?
In order to use a service account with the Google Ads API, you'll need a Google Workspace domain.
If you don't have one, I'd recommend to use the standard OAuth2 flow to obtain a refresh token of a user that has access to the Google Ads account. These tokens don't expire and can be exchanged for an access token which will authenticate your request.