Firebase Node.js 具有功能的本地测试凭证

Firebase Node.js local testing credential with functions

下面的所有代码在 firebase deploy 之后都有效,但在本地机器上 运行 时抛出错误:

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. Error code: ENOTFOUND".

如何更改以下配置,使其在部署后在本地测试环境和 Firebase 中工作?

foo.js 在本地和 firebase 上工作 getUsersFunction 适用于 firebase,但不适用于 firebase emulators:start --only=functions

我在本地设置了两个 firebase 端点并 运行 它们: firebase emulators:start --only=functions

index.js

"use strict";
const fooFunction = require("./foo");
const getUsersFunction = require("./getUsers");
const functions = require("firebase-functions");
const admin = require("firebase-admin");

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

exports.fooFunction = functions.https.onRequest((req, res) => {
   fooFunction.handler(req, res);
});

exports.getUsersFunction = functions.https.onRequest((req, res) => {
   getUsersFunction.handler(req, res, admin);
});

foo.js

exports.handler = function (req, res, database) {
   res.send("foo ran successfully");
};

getUsersFunction.js

exports.handler = function (ref, res, admin) {
   admin
      .auth()
      .listUsers(1000)
      .then(function (listUsersResult) {
         console.log(listUsersResult);
         res.send("done");
         return null;
      })
      .catch(function (error) {
         console.log("Error listing users:", error);
      });
};

我刚刚检查了 Firebase 日志并注意到了这一点:

Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

代码没有问题 - 错误是由于我的帐户限制造成的。我发布这个是希望它可以避免其他开发人员浪费我的时间。