根据文档:
如果您在 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 函数不一样吗?
我遇到了同样的问题,我解决了以下问题:
实现代码:
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
| 归档时间: |
|
| 查看次数: |
8586 次 |
| 最近记录: |