10

As per the documentation:

If you are using the Node.js Admin SDK in a Cloud Function, you can automatically initialize the SDK through the functions.config() variable:

admin.initializeApp(functions.config().firebase);

But when I try this very simple piece of code:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp(functions.config().firebase)

exports.orhub = functions.https.onRequest((req, res) => {
    res.end()
})

I get the following error:

error: 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: invalid_grant (Bad Request)\". There are two likely causes: (1) your server time is not properly synced or (2) your certificate key file has been revoked. To solve (1), re-sync the time on your server. To solve (2), make sure the key ID for your key file is still present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If not, generate a new key file at https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk."}

My Ubuntu pc has date and timezone automatically synced, so that's not the problem. I created this project today, so I got the latest modules.

So, what's the problem? Isn't "Cloud Functions" mentioned in the docs the same as Firebase functions?

4
  • 2
    Are you running this deployed to Cloud Functions, or in the local emulator via the Firebase CLI? Commented Feb 6, 2018 at 3:36
  • in the local emulator, @DougStevenson! Commented Feb 6, 2018 at 16:22
  • Do you also have gcloud installed? Commented Feb 7, 2018 at 15:01
  • Not sure, Doug, I'll try later and come back to you. Commented Feb 9, 2018 at 13:31

5 Answers 5

10

I had the same problem, and I solved it as follows:

  • Go to the firebase console / Settings / Service accounts
  • Click on the option of [ Generate new private key ]
  • Load the file to the project
  • Implementation code:

    const admin = require('firebase-admin');
    
    var serviceAccount = require('PATH/file_private_key.json');
    
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "https://DATA_BASE_URL.firebaseio.com"
    });
    

More info: https://firebase.google.com/docs/database/admin/start

Sign up to request clarification or add additional context in comments.

2 Comments

How do you perform Bullet #3 "Load the file to the project" with GCP console or Firebase Console ?
this has worked pretty well for me
4

Since version 5.9.1 of firebase-admin it's possible to call initializeApp without any arguments. See release notes here. I would suggest updating to the latest version.

2 Comments

Nor for me. But authenticating properly as per firebase.google.com/docs/admin/setup?hl=en-419 did
see my answer below
2

Firebase SDK for Cloud Functions upgrade guide: beta to version 1.0 or higher https://firebase.google.com/docs/functions/beta-v1-diff

New initialization syntax for firebase-admin firebase-admin is now initialized without any parameters within the Cloud Functions runtime.

Before (<= v0.9.1)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

Now (>= v1.0.0)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

1 Comment

This is not working for me... i get the following error: Error updating user: { 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 any hints?
0

in my case, i use GOOGLE_APPLICATION_CREDENTIALS env variable to set the path to json settings file
and in my firebase service file i write:

var admin = require("firebase-admin");

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
});

module.exports = admin

To slove this bug i need to add in app.js / server.js the line:

require('dotenv').config();

That way firebase will be able to recognize and use env variables.
all bs"d

Comments

0

I had similar problem when I was configuring my project on new PC. I checked all settings and it was proper. Finaly restarting my system helped :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.