如果您在云功能中使用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 pc有自动同步的日期和时区,所以这不是问题。我今天创建了这个项目,所以我得到了最新的模块。
所以有什么问题?文档中提到的“云功能”是否与Firebase功能相同?
从5.9.1
版本firebase-admin
开始,可以不加任何参数调用initializeApp
。 See release notes here。我建议更新到最新版本。
Firebase SDK for Cloud Functions升级指南:beta版到1.0或更高版本https://firebase.google.com/docs/functions/beta-v1-diff
firebase-admin firebase-admin的新初始化语法现在已初始化,而Cloud Functions运行时中没有任何参数。
之前(<= v0.9.1)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
现在(> = v1.0.0)
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();