Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase cannot determine project id

this is the request that I made using node


// Initialize the default app
var admin = require('firebase-admin');

var app = admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: process.env.FIREBASE_DATABASE
});

console.log(process.env.FIREBASE_DATABASE);


router.post('/', (req, res, next) => {

    app.auth().getUserByEmail("[email protected]")
    .then(function(userRecord) {
            // See the UserRecord reference doc for the contents of userRecord.
            console.log('Successfully fetched user data:', userRecord.toJSON());
            res.json(userRecord.toJSON())
        })
        .catch(function(error) {
                console.log('Error fetching user data:', error);
                res.json(error)

            });

        }

        );

I set the env var on my machine

enter image description here

for my firebase database I used env

enter image description here

given as

databaseURL: "https://fssssss.firebaseio.com",

from the firebase admin GUI ,

the error in Postman when I request this route

{
    "code": "app/invalid-credential",
    "message": "Failed to determine project ID: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND"
}

I followed the docs and google returned no results, no idea what to do. thanks.


2 Answers

Method 1 (recommended):

const firebase_admin = require('firebase-admin');
const admin = firebase_admin.initializeApp({
    credential: firebase_admin.credential.applicationDefault()
});

Then run the app like this:

GOOGLE_APPLICATION_CREDENTIALS=/path/to/yourserviceaccountkey.json node index.js

Or through a package.json script:

"start" : "GOOGLE_APPLICATION_CREDENTIALS=/path/to/yourserviceaccountkey.json node index.js"

You can also set the environmental variable likes this in a terminal session, before you run Node:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Method 2 (less secure):

Just pass the path of your service account key

const firebase_admin = require('firebase-admin');
const serviceAccount = require("/path/to/yourserviceaccountkey.json");
const admin = firebase_admin.initializeApp({
  credential: firebase_admin.credential.cert(serviceAccount);
});

References

  • Add the Firebase Admin SDK to your server
like image 53
Nouman Mukhtar Avatar answered Apr 19 '26 17:04

Nouman Mukhtar


This isn't working the way you expect:

admin.credential.applicationDefault()

You can't use this on a server that you control without additional configuration. This only works by itself when running on Google services when you want to use the default service account for your project. When running on your own server, there is no default. You have to be explicit and download service account credentials to use during initialization. At the very least, you'll need to follow the instructions in the documentation:

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:

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

When authorizing via a service account, you have two choices for providing the credentials to your application. You can either set the GOOGLE_APPLICATION_CREDENTIALS environment variable, or you can explicitly pass the path to the service account key in code. The first option is more secure and is strongly recommended.

So you will need to download the service account file, set GOOGLE_APPLICATION_CREDENTIALS correctly, then your code will work. The service account credentials are not optional.

like image 28
Doug Stevenson Avatar answered Apr 19 '26 18:04

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!