Python Firebase管理SDK DefaultCredentialsError

2024-09-28 22:25:03 发布

您现在位置:Python中文网/ 问答频道 /正文

初始化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'

我在树莓皮里面做这个

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


Tags: importjsonadminapplicationservicegooglecertificateexceptions
1条回答
网友
1楼 · 发布于 2024-09-28 22:25:03

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

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

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

相关问题 更多 >