Firebase Admin INVALID_APP_OPTIONS error at initializeApp()

Viewed 7499

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.

6 Answers

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.

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
});

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"
});

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

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,
  }),
});
Related