Firebase Admin INVALID_APP_OPTIONS error at initializeApp()
Asked Answered
T

6

7

I'm trying to connect Instagram OAuth to Firebase through Node.js back-end. I have successfully retrieved Instagram account data including access_token which I want to exchange with firebase-admin's createCustomToken on my Node.js backend. My objective here is to generate custom token so my Angular app can do signInWithCustomToken(token) into my Firebase app. There is no problem on retrieving data from Instagram as I can print my JSON object on console.

The problem is occurred when I want to exchange my access_token to Firebase Custom Token.

I have followed this guide from Firebase Admin page for Node.js and I'm facing an error message below

throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_APP_OPTIONS, "Invalid Firebase app options passed as the first argument to initializeApp() for the " +

Error: Invalid Firebase app options passed as the first argument to initializeApp() for the app named "[DEFAULT]". The "credential" property must be an object which implements the Credential interface.

Here is my code on related issue.

// authService.js

var fbAdmin = require('firebase-admin');
var serviceAccount = require('./key/key.json');

function createFirebaseToken(instagramID) {

        // I copy & pasted this var from other class
        var config = {
            apiKey: "MY_FIREBASE_APIKEY",
            authDomain: "MY_APP.firebaseapp.com",
            databaseURL: "https://MY_APP.firebaseio.com",
            storageBucket: "MY_APP.appspot.com",
        };

        console.log(fbAdmin.credential.cert(serviceAccount)); // serviceAccount successfully printed on console

        // Error appears when executing this function
        fbAdmin.initializeApp({
            serviceAccount: fbAdmin.credential.cert(serviceAccount),
            databaseURL: config.databaseURL
        });

        const uid = `instagram:${instagramID}`;
      
        // Create the custom token.
        console.log(uid);
        return fbAdmin.auth().createCustomToken(uid);
    }

It appears that my Node app cannot initialize a connection to firebase-admin but I have no idea the solution as I am a beginner on these technologies. Please advice.

Tankersley answered 30/9, 2017 at 16:18 Comment(0)
T
8

Just stumbled upon on Firebase Admin Release Notes at version 5.0.0 on May 2017 stated that serviceAccount has been removed. So instead of forcing to use serviceAccount, I use credential instead.

fbAdmin.initializeApp({
  credential: fbAdmin.credential.cert({
      projectId: '<APP_ID>',
      clientEmail: "foo@<APP_ID>.iam.gserviceaccount.com",
      privateKey: "-----BEGIN PRIVATE KEY-----\n<MY_PRIVATE_KEY>\n-----END PRIVATE KEY-----\n"
    }),
  databaseURL: config.databaseURL
});
Tankersley answered 1/10, 2017 at 3:16 Comment(0)
M
13

This issue can be resolved by updating firebase-tools, firebase-functions and firebase-admin packages to the latest version.

npm install -g firebase-tools@latest
npm install firebase-functions@latest
npm install firebase-admin@latest

Refer this GitHub page for more details.

Mailman answered 22/5, 2018 at 10:1 Comment(0)
T
8

Just stumbled upon on Firebase Admin Release Notes at version 5.0.0 on May 2017 stated that serviceAccount has been removed. So instead of forcing to use serviceAccount, I use credential instead.

fbAdmin.initializeApp({
  credential: fbAdmin.credential.cert({
      projectId: '<APP_ID>',
      clientEmail: "foo@<APP_ID>.iam.gserviceaccount.com",
      privateKey: "-----BEGIN PRIVATE KEY-----\n<MY_PRIVATE_KEY>\n-----END PRIVATE KEY-----\n"
    }),
  databaseURL: config.databaseURL
});
Tankersley answered 1/10, 2017 at 3:16 Comment(0)
N
4

Updated Answer

After continuing to work with this, it seems to be able to call admin.initializeApp() without any params, you need to make sure your `/functions nodeJs setup is up to date.

In /functions directory, run this command:

npm i --save firebase-functions@latest

Thanks to: https://mcmap.net/q/1474961/-how-to-solve-typeerror-functions-https-oncall-is-not-a-function

.. I am still getting familiar with all the nodeJS/npm details - not sure why, but seems I had to run it a few times for everything to get into place. That and the sudo npm install -g firebase-tools updated as well .

According to official docs, we need to be using at least version Firebase 4.12.0 in order to get the latest cloud functions support: https://firebase.google.com/docs/functions/callable

First Answer

I ran into the same error message in trying to set up Cloud Functions for Firebase.

Passing in the argument for admin.initializeApp solved it for my project:

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

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

After a good amount of search, gratefully found this thread with a post where it has that line of code: https://github.com/firebase/firebase-functions/issues/99 .. because so far haven't seen it anywhere in the docs about that parameter

According to the release docs for Jan. 11, 2018:

https://firebase.google.com/support/release-notes/admin/node

The admin.initializeApp() method can now be invoked without any arguments. This initializes an app using Google Application Default Credentials, and other AppOptions loaded from the FIREBASE_CONFIG environment variable.

Nursery answered 10/4, 2018 at 19:26 Comment(1)
✅ This is correct. If you're using firebase admin inside functions then you don't need to pass arguments to initializeAppBeauvais
C
4

Kindly use the following snippet, which is mentioned in the Firebase Admin SDK for Node.js. Download the JSON file from the Service Account in the Project Settings and change the path in serviceAccount. Also add your databaseUrl, which is of the form http://app_name/.firebaseio.com

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

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "yourDatabaseUrl"
});
Congo answered 7/6, 2018 at 18:38 Comment(0)
A
2

For those having this issue with "firebase-admin": "^11.0.0", you have to construct the certification passed into initializeApp() using cert imported from firebase-admin/app.

Example:

import { initializeApp, cert } from "firebase-admin/app";

const admin = initializeApp({
  credential: cert({
    projectId: process.env.MY_PROJECT_ID,
    clientEmail: process.env.MY_PRIVATE_KEY,
    privateKey: process.env.MY_CLIENT_EMAIL,
  }),
});
Aintab answered 4/7, 2022 at 7:17 Comment(0)
F
-1

Check what version of firebase-admin module you are using. As per the release note of version 5.9.1 The admin.initializeApp() method can now be invoked without a credential option. The SDK uses Google Application Default Credentials when initialized this way.

release notes

Favored answered 8/6, 2018 at 1:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.