首页 > 解决方案 > Cloud Functions Shell 中 initializeApp() 上的 Firebase 无效凭据 302

问题描述

为了加快我的开发/测试速度,我尝试开始使用 Cloud Functions:shell,而不是在每次进行微小更改时部署 --only 功能。

不过,我收到“无效凭据”错误。

这是一个基本的工作index.js 文件:

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

以下是我在 CLI 中从 shell 调用函数的方式:

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

输出是“嗨!” 写入控制台,这很好。

问题是当我尝试在以下函数中使用 admin.database() 时:

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

这是输出错误:

  "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'"

我已经阅读了 '302 found' code ad 'invalid-credentials' 的文档,但这并没有告诉我太多关于解决问题的方法,所以我开始想知道我是否需要一个服务帐户来初始化 Admin SDK,但是没有意义,因为当我运行部署时,功能无缝运行,我可以读取/写入实时数据库/从实时数据库读取/写入。

这里有什么问题?

标签: node.jsfirebaseexpressgoogle-cloud-functions

解决方案


您是否尝试过设置GOOGLE_APPLICATION_CREDENTIALS环境变量?它可能会解决您的问题:https ://firebase.google.com/docs/functions/local-emulator

在这种情况下,使用 Cloud Functions Shell 时查看此使用服务帐户凭据可能会有所帮助。


推荐阅读