0
votes

All of the code below works after firebase deploy but throws error when run on local machine:

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".

How do I change the following config so that it works in local test environment and in Firebase after deploy?

foo.js works locally and on firebase getUsersFunction works on firebase but not locally with firebase emulators:start --only=functions

I set up two firebase endpoints and run them locally with: 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);
      });
};

1
I don't think auth is emulated by the Firebase emulator suite yet. github.com/firebase/firebase-tools/issues/1677Doug Stevenson

1 Answers

0
votes

I just checked the Firebase logs and noticed this:

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

The code is fine as is - the error was due to my account restrictions. I am posting this in the hopes that it can save other developers from wasting the time I did.