Firebase身份validation失败节点JS:NotAuthorizedError
我试图通过Firebase / Node JS发送推送通知。 我目前有这个代码在Heroku上运行。
var Firebase = require('firebase'); var request = require('request'); var express = require('express'); var FCM = require('fcm-node'); var app = express(); app.set('port', (process.env.PORT || 5000)); app.use(express.static(__dirname + '/public')); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.get('/push', function(request, response) { response.send("running"); }); app.listen(app.get('port'), function() { console.log('Node app is running on port', app.get('port')); }); // Planet Firebase API Key var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key var fcm = new FCM(API_KEY); var config = { apiKey: "AIz...", authDomain: "XXX.firebaseapp.com", // Authentication > Sign-In Method > OAuth Redirect Domains databaseURL: "https://XXX.firebaseio.com/", // Database > Url at the top storageBucket: "gs://XXX.appspot.com", // Storage > Url at the top }; var firebase = Firebase.initializeApp(config); var ref = firebase.database().ref(); function listenForNotificationRequests() { var requests = ref.child('notifications'); requests.on('child_changed', function(requestSnapshot) { var objectAdded = requestSnapshot.val(); var uid = receiver["uid"] var notificationMessage = objectAdded["message"] sendNotificationToUser(uid, notificationMessage); }, function(error) { console.error(error); }); }; function sendNotificationToUser(receiverID, notificationMessage) { var message = { to: '/notifications/'+receiverID, notification: { title: "App Title", body: notificationMessage, badge: 1 } }; fcm.send(message, function(err, response){ if (response) { console.log("Successfully sent with response: ", response); } else { console.log("Something has gone wrong! Error: " + err); } }); } // start listening listenForNotificationRequests();
每当fcm.send()
被调用,我回来:
Something has gone wrong! Error: NotAuthorizedError
这导致相信Firebase没有正确初始化,但是我多次检查链接和密钥。 我做了什么不正确的?
在你的代码中,我注意到了这个部分(特别是注释):
// Planet Firebase API Key var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key var fcm = new FCM(API_KEY);
您可能会收到401 – 身份validation错误 。
使用FCM时,应始终使用服务器密钥而不是Web API密钥。
这也在Firebase控制台中显示 :项目设置> 云消息传递 >服务器密钥。
看到我的答案在这里有关不同的关键的想法。
- Google云端存储的Firebase云端functionAPI错误
- 使用Node.js和Firebase竞争条件
- Firebase Node.JS Admin SDK发送validation邮件
- Firebase错误消息/身份validation错误
- 如何从服务器端的Firebase数据库中检索上次创build的对象
- Firebase应用程序在localhost:5000 / / / app上运行
- firebase是否为每个admin.auth()。verifyIdToken(idToken)发出networking请求?
- 使用NodeJS的Firebase中的事务数据为null
- 如何检查服务器上的客户端进行身份validation?