Error 403 when connecting to Google Analytics using Python 2.7.x

I tried to get data from the Google Analytics API using the Python client (google-api-python-client). Here is the code I used:

from apiclient import discovery
from oauth2client.client import SignedJwtAssertionCredentials
from httplib2 import Http

with open("ManagementGate-622edd43c0dd.p12") as f:
    private_key = f.read()

credentials = SignedJwtAssertionCredentials(
    'XXXXXXXX@developer.gserviceaccount.com',
    private_key,
    'https://www.googleapis.com/auth/analytics.readonly')

http_auth = credentials.authorize(Http())

service = discovery.build('analytics', 'v3', http=http_auth)

result = service.data().ga().get(
      ids='ga:79873569',
      start_date='7daysAgo',
      end_date='today',
      metrics='ga:visits,ga:sessions,ga:pageviews').execute()

I created a service account on the credentials page. However, I received an error as shown below:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/analytics/v3/data/ga?metrics=ga%3Avisits%2Cga%3Asessions%2Cga%3Apageviews&alt=json&end-date=today&ids=ga%3A79873569&start-date=7daysAgo returned "User does not have any Google Analytics account.">

The following instructions are: https://developers.google.com/accounts/docs/OAuth2ServiceAccount Is there anything else I need to do? And why did I get this error? I have already enabled the API Analytics API in the API page.

+5
source share
1 answer

API Google Analytics . Google Analytics.

, Google. "" - Google Analytics. ACCOUNT, ACCOUNT Google Analytics, .

- , .

+7

Source: https://habr.com/ru/post/1676797/


All Articles