-
Troubleshooting Service Account Authentication for GA4 Analytics Data API
So, here’s what I did:
- Turned on the Google Analytics Data API in a GCP project.
- Made a service account in the credentials part of the Google Analytics Data API’s GCP console.
- Made a JSON key file, downloaded it from the service account I’d made.
- Told GOOGLE_APPLICATION_CREDENTIALS to check out the JSON key file path.
- Made the service account an admin of an Analytics account. It’s a GA4 property that I can use.
But, there’s a problem: whenever I try to run any Python script with BetaAnalyticsDataClient, I get this ‘Deadline Exceeded’ error. This happens whichever resource endpoint I try to use.
I also tried a thing from here about a global internal property, but no cigar – still the same error.
I also tried initializing the client with BetaAnalyticsDataClient.from_service_account_file(“the_file”). Didn’t work either.
I thought I’d test something, though I’ve got no idea whether it’d work:
- Used gcloud auth activate-service-account –key-file the-service-account-json-key.json.
- Gcloud auth application-default print-access-token gave me an access token.
- And then I used this:
curl -X GET -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" https://analyticsdata.googleapis.com/v1beta/properties/0/metadata
This gave me back:
{ "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "status": "PERMISSION_DENIED", "details": [ { "@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT", "domain": "googleapis.com", "metadata": { "method": "google.analytics.data.v1beta.BetaAnalyticsData.GetMetadata", "service": "analyticsdata.googleapis.com" } } ] } }
Now I don’t get it. I used this on the property where I made the service account an admin. Is there something else I need to get service accounts to work?
Log in to reply.