【问题标题】:Heroku: Firebase can't load credentialsHeroku:Firebase 无法加载凭据
【发布时间】:2019-08-27 21:05:46
【问题描述】:

问题总结

我在 Heroku 上托管使用 Firebase 的 Node.js 服务器,当我尝试在 Heroku 上运行时,我收到以下错误,即它无法加载我的凭据。

在我的本地机器上运行时它可以完美运行。我正在使用 firebase-admin npm 包来配置我的 firebase 连接/实例。

有没有人遇到过这种情况?如果是这样,我很想得到你的帮助!

Heroku 出错

错误:无法加载默认凭据。浏览至https://cloud.google.com/docs/authentication/getting-started 了解更多信息。

代码

Firebase 管理员配置文件

这是我用来配置 Firebase 管理实例的文件

const admin = require('firebase-admin');

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
  databaseURL: "https://esports-competition-2.firebaseio.com"
}); //this also allows me to use Google OAuth2 refresh token

const db = admin.firestore();

module.exports = db;

将数据保存到 Firebase 的功能

const db = require("../../configs/firebaseConfig");

async function firestorePush(userId, eventType, data) {
  try {
    //read database
    //if userId contains eventType singleEntry then remove from database
    const timeStamp = new Date();
    userId = userId.toString();
    const userDoc = db.collection("pushData").doc(userId);
    const pushData = await userDoc.set(
      {
        event: {
          eventType,
          data,
          timeStamp
        }
      },
      { merge: true }
    );
    console.log("Document set in FireStore", pushData);
  } catch (err) {
    console.log("errpr pushing to firebase", err);
  }
}

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore firebase-admin


    【解决方案1】:

    根据documentation on admin.credential.applicationDefault()

    Google 应用程序默认凭据可用于任何 Google 基础架构,例如 Google App Engine 和 Google Compute Engine。

    由于 Heroku 不是 Google 基础架构,因此您必须使用 documentation on initializing the SDK 中显示的其他选项之一来初始化 Admin SDK。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2020-04-28
      • 2017-07-13
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多