0

I am testing some Firebase Cloud functions and when I test them locally using firebase functions:shell, I get this error:

@firebase/database: 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: Error while making request: getaddrinfo ENOTFOUND 
metadata.google.internal metadata.google.internal:80. Error code: 
ENOTFOUND\"."}

It seems to happen when I try to access admin.database() inside of the function.

These are my npm modules I'm using:

"dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0",
  },

Is there something I'm missing here? I've tried uninstalling firebase-tools and reinstalling via npm i -g firebase-tools and that didn't help.

I'm also initializing the app via admin.initializeApp()

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Stevie Star
  • 2,331
  • 2
  • 28
  • 54

1 Answers1

0

I had the same problem.
If you have a look at the Admin Authentication API Errors, you'll see the error app/invalid-credential is due to:

The credential used to authenticate the Admin SDKs cannot be used to perform the desired action. Certain Authentication methods such as createCustomToken() and verifyIdToken() require the SDK to be initialized with a certificate credential as opposed to a refresh token or Application Default credential. See Initialize the SDK for documentation on how to authenticate the Admin SDKs with a certificate credential.

You need to manually specify your credentials.

To authenticate a service account and authorize it to access Firebase services, you must generate a private key file in JSON format.

To generate a private key file for your service account:

  1. In the Firebase console, open Settings > Service Accounts.
  2. Click Generate New Private Key, then confirm by clicking Generate Key.
  3. Securely store the JSON file containing the key.

I would recommend you to follow the instruction described in the section Initialize the SDK.
This solved the issue for me.

Community
  • 1
  • 1
pasine
  • 11,311
  • 10
  • 49
  • 81