【问题标题】:Google Analytics API Python Error 403 - Insufficient PermissionsGoogle Analytics API Python 错误 403 - 权限不足
【发布时间】:2021-07-11 18:56:40
【问题描述】:

我已经设置了我的分析 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()

有人可以帮帮我吗? 谢谢!

【问题讨论】:

  • 请编辑您的问题并包含您的代码
  • 完成,@DalmTo 给您带来的不便,我们深表歉意

标签: python google-api google-analytics-api google-api-python-client service-accounts


【解决方案1】:

用户对此配置文件没有足够的权限。

表示您当前正在与之进行身份验证的用户无权访问您尝试访问的帐户。

您需要像您所做的那样通过 Google 分析管理员添加用户。确保您在帐户级别添加了它。

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

User doesn't have any google analytics accounts easy solution

你错过了输入的内容。

【讨论】:

  • 我在帐户级别下从 client_secrets.json 输入了正确的电子邮件,但仍然得到相同的结果
  • 您使用的是服务帐号吗?
  • 是的,我正在使用服务帐户,并且它的状态是活动的
  • 您在console.cloud.google.com 上创建并启用了谷歌分析API。我仍然认为问题出在您使用的 VIEW id 或您添加的服务帐户电子邮件地址
猜你喜欢
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 2018-03-10
  • 2015-10-15
  • 1970-01-01
  • 2014-09-06
  • 2021-01-18
  • 2022-11-10
相关资源
最近更新 更多