问题描述
我试图使用Python客户端(google-api-python-client)从Google Analytics API获取数据。下面是我使用的代码:
来自apiclient导入发现$ b $来自oauth2client.client import来自httplib2的SignedJwtAssertionCredentials
导入Http
与开放(ManagementGate-622edd43c0dd.p12)作为f:
private_key = f.read()
credentials = SignedJwtAssertionCredentials(
'[email protected]',
private_key,
'https://www.googleapis.com/auth/analytics.readonly')
http_auth =凭证.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:会话,ga:pageviews')。execute()
我在凭证页上创建了一个服务账户。但是,我收到如下错误消息:
googleapiclient.errors.HttpError:< HttpError 403请求https:// www时.googleapis.com / analytics / v3 / data / ga?metrics = ga%3Avisits%2Cga%3Asessions%2Cga%3Apageviews& alt = json& end-date = today& id = ga%3A79873569& start-date = 7daysAgo returned用户没有任何Google Analytics帐户。>
我遵循的指示来自:
还有什么我需要做的吗?为什么我得到这个错误?我已在API页面上启用Analytics API。
您尝试使用服务帐户访问Google AnalyticsAPI。默认情况下,服务帐户无权访问任何Google Analytics帐户。
您需要做的是从Google Developer控制台取得服务帐户电子邮件地址。转到Google Analytics网站的管理部分。在帐户级别授予此服务帐户访问权限,它必须是您希望访问的Google Analytics帐户的 ACCOUNT 级别。
它不适用于网络媒体资源或视图级别,它必须是帐户级别。
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(
'[email protected]',
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/OAuth2ServiceAccountIs there anything else I need to do? And why did I get this error? I already enabled Analytics API on APIs page.
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.
这篇关于使用Python 2.7.x连接到Google Analytics时出现403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!