Learn business growth with Google Analytics 4 Forums Google Analytics 4 Troubleshooting Service Account Authentication for GA4 Analytics Data API

  • Troubleshooting Service Account Authentication for GA4 Analytics Data API

    Posted by Emily on 17 March 2023 at 10:49 am

    So, here’s what I did:

    1. Turned on the Google Analytics Data API in a GCP project.
    2. Made a service account in the credentials part of the Google Analytics Data API’s GCP console.
    3. Made a JSON key file, downloaded it from the service account I’d made.
    4. Told GOOGLE_APPLICATION_CREDENTIALS to check out the JSON key file path.
    5. 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:

    1. Used gcloud auth activate-service-account –key-file the-service-account-json-key.json.
    2. Gcloud auth application-default print-access-token gave me an access token.
    3. 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?

    Benjamin replied 1 year, 5 months ago 3 Members · 2 Replies
  • 2 Replies
  • Brooke

    Member
    12 May 2023 at 10:29 am

    It looks like your issue is related to the authentication scopes you’re using for your requests. The error message is saying that you don’t have the right permissions to do what you are trying to do.

    So, you have to use either the ‘https://www.googleapis.com/auth/analytics.readonly’ or the ‘https://www.googleapis.com/auth/analytics’ scope when you authenticate.

    Once you switch to either of these scopes, give it another shot and you should be good to go!

  • Benjamin

    Member
    28 May 2023 at 6:35 pm

    It seems like you are experiencing some difficulties trying to run a Python script using the service account you created for the Google Analytics Data API. You’ve followed the necessary steps to create and authenticate the service account, and you’ve given it the required permissions. However, the script fails to execute and produces a ‘Deadline Exceeded’ error. You’ve attempted to resolve this issue by testing various tips you found, but the problem persists.

    You also attempted to use a curl command to retrieve your API data by using an access token generated by gcloud, but you were denied due to insufficient authentication scopes error. In other words, the access token you obtained doesn’t have sufficient permissions to access the required resource.

    Whether the service account works or not may depend on other factors. There may be an issue in the way the Python script is interacting with the service account, or there might be an error in how the service account’s permissions are set up in the Google Cloud Console. It seems like the access token is not being granted the correct scopes when it’s issued.

    Remember, when you run your Python script or command, the environment must have the correct permissions to access your Google Analytics Data. If you’ve changed permissions in the console, ensure you update your local environment to reflect those changes. To sum up, the issue you’re experiencing is likely due to an authentication or permissions error.

Log in to reply.