gpt4 book ai didi

python - Google Analytics API Python 错误 403 - 权限不足

转载 作者:行者123 更新时间:2023-12-03 08:23:34 26 4
gpt4 key购买 nike

我已经设置了分析 API 和凭据,下载了 client_secrets.json 并复制了 client_email。

然后,我转到我的 Google Analytics(分析)帐户 > 属性 > 查看 > 管理 > 用户管理,并将该电子邮件添加为具有读取权限的新用户。一切都很顺利,没有错误或任何其他情况。

现在,当我尝试通过 python 使用 api 时,出现此错误:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "User does not have sufficient permissions for this profile.". Details: "User does not have sufficient permissions for this profile.">

我快要疯了。不知道发生了什么以及如何解决它。

代码:

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials


SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'client_secrets.json'
VIEW_ID = 'view_id' #exampe '12341234'


def initialize_analyticsreporting():
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
analytics = build('analyticsreporting', 'v4', credentials=credentials)
return analytics


def get_report(analytics):
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()


def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.

Args:
response: An Analytics Reporting API V4 response.
"""
for report in response.get('reports', []):
columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

for row in report.get('data', {}).get('rows', []):
dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])

for header, dimension in zip(dimensionHeaders, dimensions):
print(header + ': ', dimension)

for i, values in enumerate(dateRangeValues):
print('Date range:', str(i))
for metricHeader, value in zip(metricHeaders, values.get('values')):
print(metricHeader.get('name') + ':', value)


def main():
analytics = initialize_analyticsreporting()
response = get_report(analytics)
print_response(response)

if __name__ == '__main__':
main()

有人可以帮我吗?谢谢!

最佳答案

User does not have sufficient permissions for this profile.

表示您当前正在验证的用户无权访问您尝试访问的帐户。

您需要像之前一样通过 Google 分析管理员添加用户。确保您在帐户级别添加了它。

我建议您仔细检查是否添加了正确的用户电子邮件地址,然后检查是否使用了正确的 View ID。

User doesn't have any google analytics accounts easy solution

您错过了输入的内容。

关于python - Google Analytics API Python 错误 403 - 权限不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67130469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com