[英]Python Firebase Admin SDK, Illegal Firebase credential provided
I'm trying initialising Firebase Admin SDK using.env file on a Django app and I get the following error:我正在尝试在 Django 应用程序上使用 .env 文件初始化 Firebase Admin SDK,但出现以下错误:
Illegal Firebase credential provided. App must be initialized with a valid credential instance
the initialisation code:初始化代码:
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
firebase_admin.initialize_app({
"type": os.getenv("TYPE"),
"project_id": os.getenv("PROJECT_ID"),
"client_email": os.getenv("CLIENT_EMAIL"),
"private_key": os.getenv("PRIVATE_KEY"),
"client_id": os.getenv("CLIENT_ID"),
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
"auth_uri": os.getenv("AUTH_URI"),
"token_uri": os.getenv("TOKEN_URI"),
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
})
I found the solution that worked for me on a comment on this post: Initializing Firebase Admin via Environment Variables without storing serviceAccount.json我在这篇文章的评论中找到了对我有用的解决方案: Initializing Firebase Admin via Environment Variables without storing serviceAccount.json
Basically I stored all the firebase credentials json in one.env variable (it should be in one line) and used it like this:基本上我将所有 firebase 凭据 json 存储在 one.env 变量中(它应该在一行中)并像这样使用它:
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
firebaseConfig = json.loads(os.getenv("FIREBASE_CREDENTIALS", "{}"))
class FCMController:
def __init__(self):
if not firebase_admin._apps: # check if firebase_admin is initialized
firebase_admin.initialize_app(
credential=credentials.Certificate(firebaseConfig))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.