python firestore问题与身份验证

问题描述 投票:1回答:1

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

使用以下代码

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')

并得到以下错误

google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置GOOGLE_APPLICATION_CREDENTIALS或明确创建凭据并重新运行该应用程序。欲获得更多信息

我想这是默认凭据有问题,我没有得到的是,如果我在我的代码中初始化凭据,为什么应用程序不断抛出默认凭据的错误?我明确地给它正确的信用档案。

python firebase google-cloud-firestore firebase-admin
1个回答
2
投票

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

credential_path = "D:\****.json"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

不要忘记通过import os导入库os

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

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