node.js - 火力警告 : Provided authentication credentials are invalid

标签 node.js firebase

我正在尝试在 node.js 中使用 Firebase,但每次重新启动服务器时都会出现以下错误:

FIREBASE WARNING: Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your apiKey and databaseURL match the values provided for your app at https://console.firebase.google.com/, or if you're using a service account, make sure it's authorized to access the specified databaseURL and is from the correct project.

以下是我的 index.js:_

var express = require('express');
var router = express.Router();
var mongoose=require('mongoose');
var admin=mongoose.model('admin');


var firebase = require("firebase");

// Initialize the app with no authentication
firebase.initializeApp({
	  serviceAccount: {
    projectId: "...",
    clientEmail: "...",
    privateKey: "-----BEGIN PRIVATE KEY-----...",
  },

      databaseURL: "..."
});

console.log("sfsaf")

// The app only has access to public data as defined in the Security Rules
			var db = firebase.database();
			var ref = db.ref("unitalk-b9145");
			var messagesRef = ref.child("messages");
			
			messagesRef.push({
				name:"Rupali",
				post:"Demo test of firebase"
			});

虽然我已经检查了服务帐户和数据库 URl 的路径.. 请帮助..

最佳答案

您无法使用“firabase”包使用服务帐户登录。为此,您需要使用“firabase-admin”包。您可以在此处找到详细信息 ( https://firebase.google.com/docs/database/admin/start )。

更新时间:2016 年 11 月 8 日

转到:https://console.firebase.google.com

要使用 Firebase Admin SDK,您需要一个 Firebase 项目、一个用于与 Firebase 服务通信的服务帐户,以及一个包含您的服务帐户凭据的配置文件。

  1. 导航到项目设置中的“服务帐户”选项卡 页面。
  2. 选择您的 Firebase 项目。如果您还没有,请单击 创建新项目按钮。如果您已经有一个现有的 与您的应用关联的 Google 项目,点击导入 Google 项目 相反。
  3. 点击底部的生成新私钥按钮 “服务帐户”选项卡的 Firebase Admin SDK 部分。
  4. 点击按钮后,一个包含您的服务的 JSON 文件 帐户的凭据将被下载。你需要这个 在下一步中初始化 SDK。

示例代码;

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

关于node.js - 火力警告 : Provided authentication credentials are invalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462941/

相关文章:

javascript - 将大数组传递给 Node 子进程

node.js - 我已经用brew更新了yarn,但我无法再运行yarn命令

javascript - 从对象列表中一次编辑一个对象的正确 Angular/Firebase 方法是什么?

ios - Facebook 登录 UIView 加载异常,而 UI 行为符合预期

Firebase 性能监控(角度)

firebase - Access-Control-Allow-Origin 请求存储文件

javascript - 给定一个 JSON 对象数组,如何根据嵌套值获取 JSON 对象 X?

node.js - AWS 负载均衡器安全组

javascript - MongoDB 连接期间的 Mocha 超时

java - 检查 Firebase Cloud Messaging 上的新通知。 (流式细胞术)