python firestore 身份验证问题

the*_*ter 2 python firebase firebase-admin google-cloud-firestore

我正在使用 python 和 firestore,并尝试在后端创建一个客户端。我正在关注这个教程

用下面的代码

import firebase_admin
from firebase_admin import credentials, firestore
cred = credentials.Certificate("cred_file.json")
firebase_admin.initialize_app(cred)
db = firestore.Client()
ref = db.collections(u'table')
Run Code Online (Sandbox Code Playgroud)

并收到以下错误

google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式创建凭据并重新运行应用程序。了解更多信息

我猜想默认凭据有问题,我不明白的是,如果我在代码中初始化凭据,为什么应用程序会不断抛出默认凭据的错误?我明确地给它正确的信用文件。

小智 6

您需要下载服务帐户密钥(JSON 文件),然后将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为指向设备上的该文件。

credential_path = "D:\****.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
Run Code Online (Sandbox Code Playgroud)

不要忘记导入库操作系统import os

有关更多详细信息,请参阅https://cloud.google.com/firestore/docs/quickstart-servers 。