使用 Python 2.7.x 连接到 Google Analytics 时出现 403 错误 [英] Got 403 error when connecting to Google Analytics with Python 2.7.x

查看:19
本文介绍了使用 Python 2.7.x 连接到 Google Analytics 时出现 403 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Python 客户端 (google-api-python-client) 从 Google Analytics API 获取数据.这是我使用的代码:

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.">

我遵循的说明来自:https://developers.google.com/accounts/docs/OAuth2ServiceAccount还有什么我需要做的吗?为什么我收到这个错误?我已经在 API 页面上启用了 Analytics API.

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.

推荐答案

您正在尝试使用服务帐户访问 Google Analytics API.默认情况下,服务帐户无权访问任何 Google Analytics(分析)帐户.

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.

您需要做的是从 Google Developer Console 获取服务帐户电子邮件地址.转到 Google Analytics 网站的管理部分.在 ACCOUNT 级别授予此服务帐户访问权限,它必须是您要访问的 Google Analytics(分析)帐户的ACCOUNT 级别.

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.

这篇关于使用 Python 2.7.x 连接到 Google Analytics 时出现 403 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆