【问题标题】:Firebase invalid-credential 302 on initializeApp() in Cloud Functions ShellCloud Functions Shell 中 initializeApp() 上的 Firebase 无效凭据 302
【发布时间】:2019-11-15 13:54:27
【问题描述】:

为了加快我的开发/测试速度,我尝试开始使用 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")

输出是 "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”代码和“invalid-credentials”的文档,但这并没有告诉我太多关于解决问题的信息,所以我开始想知道我是否需要一个服务帐户来初始化 Admin SDK ,但这没有任何意义,因为当我运行 deploy 时,函数会无缝运行,并且我可以读取/写入 Realtime DB。

这里有什么问题?

【问题讨论】:

    标签: node.js firebase express google-cloud-functions


    【解决方案1】:

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

    查看Use of Service account credentials when using Cloud Functions Shell 在这种情况下可能会有所帮助。

    【讨论】:

    • 嗨,Kian,谢谢。使用服务帐户解决了这个问题,虽然我很想知道为什么在部署时不需要这样做,但是没有它,shell 会失败。
    • 这就是你的谷歌图书馆。当您尝试从 Google 集成它自己不提供的东西时,您可能会遇到很多问题,class not found 异常。
    • 嗨@RedKnight91,我很高兴它有帮助。但毕竟你是对的,这有点违反直觉,你需要做这个变通方法来克服它。
    猜你喜欢
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2019-03-20
    • 2018-02-20
    • 2018-10-09
    • 1970-01-01
    • 2021-06-15
    相关资源
    最近更新 更多