1man
1man

Reputation: 5634

'auth/invalid-api-key' When Letting the Admin SDK discover a service account

In different pages of the Firebase Admin SDK documentation, e.g., this page, it is suggested that:

If your code is deployed in an environment managed by Google, the Admin SDK can attempt to auto-discover... the service account provisioned for your app...To make use of these signing methods, initialize the SDK with Google Application Default credentials and do not specify a service account ID string: admin.initializeApp();

When I do this, I get the following error message:

[Error: Your API key is invalid, please check you have copied it correctly.] code: 'auth/invalid-api-key', message: 'Your API key is invalid, please check you have copied it correctly.'

Note that I do not get this error message when I manually download and import the credentials and service account JSON files in my project.

Detailed information for reproduction of the error: 1- I'm deploying this on Cloud Functions using Firebase CLI. So, basically, I use firebase deploy.

2- Here is the minimal code in my Node.js App:

const admin = require("firebase-admin");
const config = require("./firebase-config");
admin.initializeApp();

const firebase = require("firebase");
firebase.initializeApp(config);

const functions = require("firebase-functions");
const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.json());

const cors = require("cors");
app.use(cors());

app.get("/", Some_Function);

exports.api = functions.https.onRequest(app);

The error happens when I replace firebase.initializeApp(config); with firebase.initializeApp();

Upvotes: 0

Views: 4897

Answers (2)

Eric
Eric

Reputation: 1

I have tried

  • dotenv did not work
  • process.env.PRIVATE_KEY.replace(/\\n/g, '\n') did not work

Step1: go to the console and generate new service account file

Step2: export GOOGLE_APPLICATION_CREDENTIALS='path/to/serviceAccount.json' (it doesn't matter where it is, e.g : 'user/username/download/serviceAccount.json')

Step3: if(!admin.apps.length) admin.initializeApp(); In the docs, either export the json object as param or no param with step 2

Upvotes: 0

Hiranya Jayathilaka
Hiranya Jayathilaka

Reputation: 7438

You're trying to initialize the Firebase Client SDK in a server-side environment:

const firebase = require("firebase");
firebase.initializeApp(config);

The client SDK accepts a whole different set of credentials compared to firebase-admin. You can initialize firebase-admin without any arguments in GCP managed environments (e.g. Cloud Functions, Cloud Run), but the same doesn't apply to firebase. You need to provide a valid client app configuration obtained from your Firebase project.

const admin = require('firebase-admin');
admin.initializeApp(); // This is ok

const firebase = require('firebase');
firebase.initializeApp(); // This is wrong

See https://firebase.google.com/docs/web/setup#config-object for details on how to obtain a client app configuration.

Also please note that using the client SDK in an environment like Functions is rather unusual. I'd advise you rethink your use case, and see if you really need to use firebase client SDK in your function.

Upvotes: 5

Related Questions