Issue
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.
Solution
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
Answered By - Edward
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.