Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google Analytics API Python错误403-权限不足_Python_Google Api_Google Analytics Api_Google Api Python Client_Service Accounts - Fatal编程技术网

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

Google Analytics API Python错误403-权限不足,python,google-api,google-analytics-api,google-api-python-client,service-accounts,Python,Google Api,Google Analytics Api,Google Api Python Client,Service Accounts,我已经设置了分析api和凭据,下载了client_secrets.json并复制了client_电子邮件 然后,我转到我的Google Analytics帐户>属性>视图>管理员>用户管理,并将该电子邮件添加为具有读取权限的新用户。 这一切都很顺利,没有任何错误 现在,当我试图通过python使用api时,我得到以下错误: googleapiclient.errors.HttpError: <HttpError 403 when requesting https://analyticsre

我已经设置了分析api和凭据,下载了client_secrets.json并复制了client_电子邮件

然后,我转到我的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.">
有人能帮我吗? 谢谢

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

表示您当前正在进行身份验证的用户没有访问您尝试访问的帐户的权限

您需要通过Google analytics管理员添加用户,正如您所做的那样。确保您在帐户级别添加了它

我建议您再次检查是否添加了正确的用户电子邮件地址,然后检查是否使用了正确的视图id


您没有键入任何内容。

我已在帐户级别下从client_secrets.json输入了正确的电子邮件,但仍然得到相同的结果。您使用的是服务帐户吗?是的,我使用的是服务帐户,其状态为active,您创建了并启用了google analytics api。我仍然认为问题在于您正在使用的视图id或您添加的服务帐户电子邮件地址。请编辑您的问题并包括您的代码完成,@DalmTo对于由此带来的不便,深表歉意
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()