【问题标题】:python firestore issue with authenticationpython firestore问题与身份验证
【发布时间】:2019-07-23 15:43:06
【问题描述】:

我正在使用带有 firestore 的 python,并尝试在后端创建一个客户端。我正在关注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】:

您需要下载服务帐户密钥(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

【讨论】:

  • 我拿到了密钥,但问题是,如果我在代码中显式设置身份验证文件,为什么还需要设置 env 变量?
  • 在您自己的服务器上初始化下看到这个firebase.google.com/docs/firestore/quickstart - 有什么区别?我关注了这个
猜你喜欢
  • 1970-01-01
  • 2016-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2016-10-16
相关资源
最近更新 更多