【问题标题】:Python Firebase Admin SDK - DefaultCredentialsErrorPython Firebase Admin SDK - DefaultCredentialsError
【发布时间】:2021-07-29 23:15:54
【问题描述】:

我在初始化 Firebase Admin SDK for Python 时无法设置凭据。从here 获得灵感,我是这样设置的:

import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate('./serviceAccountKey.json')
firebase_admin.initialize_app(cred)
db = firestore.Client()

...但这会导致:

raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.

我可以让它工作的唯一方法是首先创建一个像这样的GOOGLE_APPLICATION_CREDENTIALS 环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="serviceAccountKey.json"

对于credentials.Certificate(path)的路径,我试过'./serviceAccountKey.json''serviceAccountKey.json',甚至是绝对路径'/home/pi/projects/serviceAccountKey.json'

我在 Raspberry Pi 中执行此操作。

如何在不显式设置GOOGLE_APPLICATION_CREDENTIALS 环境变量的情况下使凭据工作?

【问题讨论】:

  • 你试过把你的json文件直接放到initialize_app()方法中吗?通常不需要将其创建为证书,因为它依赖于不同的数据结构
  • 我刚刚试了一下,得到了这个错误:ValueError: Illegal Firebase credential provided. App must be initialized with a valid credential instance.

标签: python firebase-authentication raspberry-pi firebase-admin


【解决方案1】:

您可以使用environ在本地设置环境变量

import os
>>> os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
    './serviceAccountKey.json'

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './serviceAccountKey.json'

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 2018-03-20
    • 2021-11-17
    • 1970-01-01
    • 2019-05-16
    • 2018-05-17
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多