簡體   English   中英

Google Analytics API Python 錯誤 403 - 權限不足

[英]Google Analytics API Python Error 403 - Insufficient Permissions

我已經設置了我的分析 api 和憑據,下載了 client_secrets.json 並復制了 client_email。

然后,我轉到我的 Google Analytics(分析)帳戶 > 屬性 > 視圖 > 管理員 > 用戶管理並添加 email 作為具有讀取權限的新用戶。 這一切都很好,沒有錯誤或任何錯誤。

現在,當我嘗試通過 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()

有人能幫助我嗎? 謝謝!

用戶對此配置文件沒有足夠的權限。

表示您當前正在與之進行身份驗證的用戶無權訪問您嘗試訪問的帳戶。

您需要像您所做的那樣通過 Google 分析管理員添加用戶。 確保您在帳戶級別添加了它。

我建議您仔細檢查您是否添加了正確的用戶 email 地址,然后檢查您是否使用了正確的視圖 ID。

用戶沒有任何谷歌分析帳戶簡單的解決方案

你錯過了輸入的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM