我正在尝试在 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 服务通信的服务帐户,以及一个包含您的服务帐户凭据的配置文件。
- 导航到项目设置中的“服务帐户”选项卡 页面。
- 选择您的 Firebase 项目。如果您还没有,请单击 创建新项目按钮。如果您已经有一个现有的 与您的应用关联的 Google 项目,点击导入 Google 项目 相反。
- 点击底部的生成新私钥按钮 “服务帐户”选项卡的 Firebase Admin SDK 部分。
- 点击按钮后,一个包含您的服务的 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/