【问题标题】:firebase-admin on firebase-functions is unable to initializeApp correctlyfirebase-functions 上的 firebase-admin 无法正确初始化 App
【发布时间】:2020-04-18 05:23:04
【问题描述】:

我试图使用 firebase 云功能和 firebase-admin 组合来为我在 auth 中的用户添加 auth 声明创建路由。 我正在使用 firebase serve 对其进行测试,并尝试了以下代码:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const func = () => {
    admin.auth().setCustomUserClaims("(req.params.uid", {isActivated: true}).then(() => {
        console.log("Done")
        })
        .catch(e => console.error(e))
}
setTimeout(func, 4000)


// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.api = functions.https.onRequest((request, response) => {
console.log(functions.config())
 response.send("Hello from Firebase!");
});

这实际上并没有起作用,并给了我以下错误:

{ Error: 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: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".
>      at FirebaseAppError.FirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:42:28)
>      at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:88:28)
>      at new FirebaseAppError (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:123:28)
>      at C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\firebase-app.js:121:23
>      at process._tickCallback (internal/process/next_tick.js:68:7)
>    errorInfo:
>     { 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: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".' },
>    codePrefix: 'app' }

一篇博文说,如果我想通过 firebase-admin 使用 firestore 和实时数据库以外的任何东西,我必须做一些额外的配置,所以我尝试使用以下代码测试 firestore 是否正常工作:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

exports.api = functions.https.onRequest((req, res) => {
    var db = admin.firestore();
    db.collection('users').get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    }); 
  res.send("Hi")
});

它又给了我另一个与管理员凭据本身相关的错误:

Error getting documents Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:159:19)
>      at process._tickCallback (internal/process/next_tick.js:68:7)

functions.config() 返回{}

发生的一切都发生在命令 firebase serve 上。 下面的代码适用于 serviceAccount.json 文件:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://status-kgi.firebaseio.com"
});

但我认为必须对云功能使用这种管理员初始化并不是一个好主意。

【问题讨论】:

  • 请编辑问题以非常具体地说明您正在做什么以获取此错误。您是在 firebase 模拟器中本地运行吗?
  • @DougStevenson 我在笔记本电脑上运行 firebase serve
  • 刚刚运行 *firebase init 选择云函数和托管,然后将上述代码添加到 index.js 文件中,并将 .api 函数添加到 firebase.json
  • 这是一个常见问题。如果您搜索该错误消息,您会发现很多信息。如果您没有看到解决问题的问题,请向 firebase-tools 提出问题。 github.com/firebase/firebase-tools/…
  • @DougStevenson 已经尝试了很多搜索,但没有找到特定案例的结果。这个文件中根本没有巨大的代码。只是模块导入、管理员初始化和 http 触发器。就这样。我希望我的小测试代码没有任何错误。

标签: google-cloud-firestore firebase-authentication google-cloud-functions firebase-admin firebase-cli


【解决方案1】:

来自 Github 问题解决方案:

查看一些旧问题,我相信这已在版本 8.5.0 中由 #2214 修复

解决方法如下:

const admin = require('firebase-admin');
admin.initializeApp(); // no neeed for functions.config().firebase

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2018-09-16
    相关资源
    最近更新 更多