google.auth.exceptions.DefaultCredentialsError [Python]

xt0899hw  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(323)
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中
我真的迷路了,所以我不知道该尝试什么。

7fyelxc5

7fyelxc51#

这是因为,正如documentation所解释的,方法BetaAnalyticsDataClient()确实接受了一个可选的options对象,该对象内部有credentials
无论是否提供,它都将搜索默认值(在您的情况下没有示例化)。
要指定它们,只需:

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

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

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

或者,如果您的计算机中有gcloud SDK,您可以访问错误消息中的建议链接并设置默认凭据!
希望它能真正解决你的问题。我没有Google Analytics(分析)凭据,因此无法测试它。

相关问题