0

In order to speed up my development/testing, I am trying to get started with using the Cloud Functions:shell instead of deploying --only functions everytime I make a tiny change.

I am getting an 'invalid-credentials' error though.

This is a basic working index.js file:

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

const app = express();
app.get("/say_hi", (req, res) => console.log("HI!"));
exports.oauth = functions.https.onRequest(app);

Here is how I call the function from the shell in the CLI:

> firebase functions:shell
> oauth.get("say_hi")

Output is "HI!" written to the console, which is good.

The problem is when I try using admin.database() as in the following function:

app.get("/get_ref", (req, res) => admin.database().ref("users"));

This is the output error:

  "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: 302 Found'"

I've read the docs for '302 found' code ad 'invalid-credentials', but this doesn't tell me much about solving the issue, so I started wondering if I need a service account to initialize the Admin SDK, but that would not make sense as when I run deploy the functions run seamlessly and I can read/write to/from the Realtime DB.

What is the issue here?

RedKnight91
  • 340
  • 3
  • 17

1 Answers1

0

Have you tried to set the GOOGLE_APPLICATION_CREDENTIALS env variable? it probably would solved your issue: https://firebase.google.com/docs/functions/local-emulator

and looking at this Use of Service account credentials when using Cloud Functions Shell might be helpful in this case.

Kian
  • 314
  • 2
  • 7
  • Hi Kian, thank you. Using a Service Account solves the issue, although I am curious to know why this is not required when deploying but the shell fails without it. – RedKnight91 Nov 15 '19 at 14:52
  • That's Google libraries for you. You'll probably end up with a lot of issues, class not found exceptions when you try to integrate something from Google which it does not provide itself. – Furkan Yurdakul Nov 15 '19 at 14:55
  • Hi @RedKnight91, I am glad that it helps. But after all you are right about this being sort of counter-intuitive which you need to do this workaround to overcome it. – Kian Nov 15 '19 at 15:01