Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. ERROR HAPPENS SOMETIMES?

To preface, this is the error.

Promise { <pending> }
nodejsproject\node_modules\firebase-admin\lib\utils\error.js:44
        var _this = _super.call(this, errorInfo.message) || this;
                           ^
FirebaseAppError: 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".
    
  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. Error code: ENOTFOUND".'
  },
  codePrefix: 'app'
}

From my understanding, this error is because my params are invalid or something causing an improper initialization of firebase.

Now I am initializing the app like this: initializeApp();.

So the initialization works. When I run a command such as const userInfo = await getAuth().verifyIdToken(authorization); the command works and I get userInfo but when I run a command such as const userInfo = await getAuth().getUser(uuid); it fails.

To my knowledge, the syntax is right and how I'm using the methods are right as well. I'm just unsure why I get an initializeApp error when using one method but not another.

like image 226
Edward Avatar asked Sep 01 '25 10:09

Edward


2 Answers

I figured out how to get both functions to work without throwing an error. To fix my problem, I changed my env variables.

Before, I had:

GCLOUD_PROJECT="project-name"
FIREBASE_CONFIG={keyDetailsHere}

After, I change it to where the value is the path to the service account key:

GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

I am unsure WHY the original one worked on some functions (particularly getAuth().verifyIdToken()) but failed on others. As far as my understanding goes, initializeApp(), if left blank, checks for the environment variable GOOGLE_APPLICATION_CREDENTIALS. If it finds it, it uses the credentials that you explicitly set, if not, it uses the Google Application Default Credentials. After being set up with the Google Application Default Credentials, it looks at the FIREBASE_CONFIG environment variable for initialization options.

An excerpt from the documentation: "Because default credentials lookup is fully automated in Google environments, with no need to supply environment variables or other configuration, this way of intializing the SDK is strongly recommeneded for applications running on Compute Engine, Kubernetes Engine, App Engine, and Cloud Functions."

My understanding is that my original setup did not work because I'm developing locally and not in a Google environment so that it wouldn't be able to grab a proper Google Application Default Credential.

For reference, here are the docs: LINK

like image 188
Edward Avatar answered Sep 04 '25 01:09

Edward


In this error reason is json Credential implement or not properly import

Firebase json is not import or applicationDefault() token expired, so now you import your fcm json file.

Follow this step ->

import json from "your-jsonfile-028f728f9457.json" assert { type: "json" };

OR

import fs from "fs";

let jsonData = JSON.parse(
  fs.readFileSync("./your-jsonfile-028f728f9457.json", "utf-8")
);

in this jsonData to use your credential

admin.initializeApp({
  credential: admin.credential.cert(jsonData),
  projectId: "fcm=project-id",
});
like image 29
MYOUSUFFS Avatar answered Sep 03 '25 23:09

MYOUSUFFS