根据 the documentation :
如果您在 Cloud Functions 中使用 Node.js Admin SDK,您可以通过 functions.config() 变量自动初始化 SDK:
admin.initializeApp(functions.config().firebase);
但是当我尝试这段非常简单的代码时:
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)
exports.orhub = functions.https.onRequest((req, res) => {
res.end()
})
我收到以下错误:
error: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: invalid_grant (Bad Request)\". There are two likely causes: (1) your server time is not properly synced or (2) your certificate key file has been revoked. To solve (1), re-sync the time on your server. To solve (2), make sure the key ID for your key file is still present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If not, generate a new key file at https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk."}
我的 Ubuntu 电脑已自动同步日期和时区,所以这不是问题所在。我今天创建了这个项目,所以我得到了最新的模块。
那么,问题是什么?文档中提到的“Cloud Functions”不是和 Firebase 函数一样吗?
最佳答案
我遇到了同样的问题,我是这样解决的:
- 转到 firebase 控制台/设置/服务帐户
- 点击【生成新私钥】选项
- 加载文件到项目
实现代码:
const admin = require('firebase-admin'); var serviceAccount = require('PATH/file_private_key.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://DATA_BASE_URL.firebaseio.com" });
关于node.js - Firebase 应用程序初始化失败,凭据无效(firebase 函数初始化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48634930/