8

I tried to get data from Google Analytics API with Python client(google-api-python-client). Here's 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 Credentials Page. However, I got an error as 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 instructions I followed are from: https://developers.google.com/accounts/docs/OAuth2ServiceAccount Is there anything else I need to do? And why did I get this error? I already enabled Analytics API on APIs page.

david30xie
  • 946
  • 3
  • 12
  • 23
  • @alecxe I tried, same error here. P12 file is not simple text file, so I don't think striping it may help. Thanks anyway! – david30xie Dec 28 '14 at 07:23
  • This Q is duplicate of this https://stackoverflow.com/questions/12837748/analytics-google-api-error-403-user-does-not-have-any-google-analytics-account – Trect Aug 25 '19 at 15:00

1 Answers1

14

You are trying to access the Google Analytics API using a service account. A service account by default does not have access to any Google Analytics accounts.

What you need to do is take the Service account email address from the Google Developer console. Go to the Admin section of the Google Analytics Website. Give this service account access at the ACCOUNT level it must be the ACCOUNT level to the Google Analytics account you wish to access.

It wont work at the Web property or the view level it must be the Account level.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    I just gave 'Read&Analyze' permission to my service account, but it still gave me same error. I did this by logging into my Google Analytics, and click on Admin, and then user management. Is that correct? – david30xie Dec 29 '14 at 01:54
  • 1
    It works! Seems like it takes a while after adding permissions. Thank you, @DalmTo – david30xie Dec 29 '14 at 02:01
  • Any chance, you could add a little detail on how to grant said permissions to my service account? Do I add the email address as an user? – Fabian Bosler Mar 13 '18 at 16:47
  • 1
    @FabianBosler yes add it as a user at the account level. Service accounts are preauthorized – Linda Lawton - DaImTo Apr 19 '18 at 10:23