Search code examples
pythongoogle-apigoogle-analytics-apigoogle-api-python-clientservice-accounts

Google Application Credentials Python (Jupyter Notebook)


I'm trying to start using Google Analytics API 4 as instructed with Python and Jupyter Notebook. I follow the instructions https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries and get to Step 3. Configure authentication

And then they write that you need to set GOOGLE_APPLICATION_CREDENTIALS="[PATH]" I downloaded this file to my computer and added it to the project folder, but I can't get verified using the service account.

On github they write https://github.com/googleapis/python-analytics-data#installation that you need to use a virtual environment? Is it so? Will it work without it?

I am using service-account, not oauth 2.0


Solution

  • I did. I'll tell you how it turns out for beginners:

    https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries

    • Click on the blue button Enable the Google Analytics Data API v1, a project is created, a service account is created in Google Cloud and the Google Analytics API is activated. Download JSON file

    • After that, you need to add a service account in the GA4 resource

    • Then you need to authenticate according to the method from here https://cloud.google.com/docs/authentication/production#setting_the_environment_variable This requires Passing credentials manually. Using the code in the article, the path to the json file on the computer is indicated

    When trying to authenticate a service account, 403 GET storage.googleapis.com/storage/v1/… will be issued: starting-account-smhpwtovr5jj @test-data-api-1654114095791.iam.gserviceaccount.com does not have storage.buckets.list access to the Google Cloud project.

    To do this, you need to go to IAM and create a new role for the Owner or Storage Admin service account. After that, GOOGLE_APPLICATION_CREDENTIALS finds and you can start installing the Data API library