Firebase 应用初始化失败,凭证无效(firebase 函数初始化)

AFM*_*les 7 node.js firebase

根据文档

如果您在 Cloud Function 中使用 Node.js Admin SDK,则可以通过 functions.config() 变量自动初始化 SDK:

admin.initializeApp(functions.config().firebase);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试这段非常简单的代码时:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase)

exports.orhub = functions.https.onRequest((req, res) => {
    res.end()
})
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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."}
Run Code Online (Sandbox Code Playgroud)

我的 Ubuntu 电脑已自动同步日期和时区,所以这不是问题。我今天创建了这个项目,所以我得到了最新的模块。

所以有什么问题?文档中提到的“云函数”与 Firebase 函数不一样吗?

JxD*_*gel 5

我遇到了同样的问题,我解决了以下问题:

  • 转到 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"
    });
    
    Run Code Online (Sandbox Code Playgroud)

更多信息:https : //firebase.google.com/docs/database/admin/start


Pet*_*ter 4

由于版本5.9.1可以firebase-admininitializeApp没有任何参数的情况下调用。请参阅此处的发行说明。我建议更新到最新版本。