google.auth.exceptions.DefaultCredentialsError [Python]

问题描述 投票:0回答:1
import json
import csv
import google.analytics.data


# Load the JSON key file
with open('key.json', 'r') as f:
    service_account_info = json.load(f)

# Create a service account object
service_account = service_account_info['client_email']

# Create a Google Analytics Reporting API client
client = google.analytics.data.BetaAnalyticsDataClient()


# Set the view ID
view_id = '74466483'

# Set the date range
start_date = '2023-01-01'
end_date = '2023-05-01'

# Define the metrics and dimensions
metrics = ['ga:sessions', 'ga:bounce_rate', 'ga:average_time_on_page', 'ga:entrances', 'ga:pageviews', 'ga:unique_pageviews']
dimensions = ['ga:source', 'ga:page']

# Run the report
report = client.run_report(
    view_id,
    start_date,
    end_date,
    metrics,
    dimensions
)

# Write the report to a CSV file
with open('report.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(report.header)
    for row in report.rows:
        writer.writerow(row)

ERROR:


$ python CompanyName.py 
Traceback (most recent call last):
  File "C:\Users\Daniel Kamen\Desktop\CompanyNameCode\CompanyName.py", line 14, in <module>
    client = google.analytics.data.BetaAnalyticsDataClient()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\google\analytics\data_v1beta\services\beta_analytics_data\client.py", line 426, in __init__
    self._transport = Transport(
                      ^^^^^^^^^^
  File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\google\analytics\data_v1beta\services\beta_analytics_data\transports\grpc.py", line 148, in __init__
    super().__init__(
  File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\google\analytics\data_v1beta\services\beta_analytics_data\transports\base.py", line 100, in __init__
    credentials, _ = google.auth.default(
                     ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\MYNAME\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\google\auth\_default.py", line 648, in default
    raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

我获得了对我公司的谷歌分析帐户的管理员访问权限,所以我真的不知道运行它需要什么。目标是将公司数据导出成csv文件,以帮助我们将旧数据转换为GA4

我真的迷路了,所以我不知道该尝试什么。

python google-analytics-api
1个回答
0
投票

那是因为,正如 documentation 所解释的那样,方法

BetaAnalyticsDataClient()
确实接受一个可选的
options
对象,里面有
credentials

无论是否提供,它都会搜索默认的(在您的情况下未实例化)。

要指定它们,只需:

client = google.analytics.data.BetaAnalyticsDataClient(options={"credentials":{"client_email": service_account, "private_key": "YOUR_KEY"}})

或者,为了更具可读性,在外部创建一个字典并调用它。

credentials_dict = {
    "options": {
        "client_email": service_account,
        "private_key": "YOUR_KEY"
    }
}
client = google.analytics.data.BetaAnalyticsDataClient(options=credentials_dict)

或者,如果您的计算机中有

gcloud
SDK,您可以访问错误消息中建议的链接并设置默认凭证!

希望它真的能解决你的问题。我没有 Google Analytics 凭据,因此无法对其进行测试。

© www.soinside.com 2019 - 2024. All rights reserved.