Search code examples
google-analyticsgoogle-analytics-apigoogle-analytics-4

Getting different errors when try to run report for GA4 with regular account


I am trying to run a simple report on GA4 by using Google Analytics Data API Python client with a regular user credentials:

    request = RunReportRequest(
        property=f"properties/11111",
        dimensions=[Dimension(name=f['name']) for f in report_definition['dimensions']],
        metrics=[Metric(name=f['expression']) for f in report_definition['metrics']],
        date_ranges=[DateRange(start_date=date, end_date=date)],

    )
    response = client.run_report(request)

And the client is BetaAnalyticsDataClient as also mentioned in the documentation:

        credentials = Credentials(
            token=None,
            refresh_token=config['refresh_token'],
            client_id=config['client_id'],
            client_secret=config['client_secret'],
            token_uri="https://accounts.google.com/o/oauth2/token",
            scopes=["https://www.googleapis.com/auth/analytics.readonly"]
        )

        client = BetaAnalyticsDataClient(credentials=credentials)

It is not a Service Account so I am using google.oauth2.credentials.Credentials class as same in other Google APIs.

However, this operation is throwing an exception during the run_report function:

grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "Getting metadata from plugin failed with error: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})"
    debug_error_string = "UNKNOWN:Error received from peer analyticsdata.googleapis.com:443 {created_time:"2023-01-14T14:12:10.907813+03:00", grpc_status:14, grpc_message:"Getting metadata from plugin failed with error: (\'invalid_grant: Bad Request\', {\'error\': \'invalid_grant\', \'error_description\': \'Bad Request\'})"}"
>

And when I try to use my access token in the credentials:

credentials = Credentials(
                token=config["token"],
                refresh_token=config['refresh_token'],
                client_id=config['client_id'],
                client_secret=config['client_secret'],
                token_uri="https://accounts.google.com/o/oauth2/token",
                scopes=["https://www.googleapis.com/auth/analytics.readonly"]
            )

This time I am getting following error:

google.api_core.exceptions.Unauthenticated: 401 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.

I am sure that my credentials is correct since I am using same account in my other repos.

Also, note that, I tried same operation with a service account and it does not give any error. However, for this purpose, I need to use a regular developer account since the OAuth flow is on a frontend project.

What are the suggestions on that issue? Is it possible to use a developer account in here and if yes, how?


Solution

  • I was able to fix the issue. The app just needs a sign-out sign-in (or refreshing the access token).