【问题标题】:Firebase Authentication Failure on Node JS: NotAuthorizedErrorNode JS 上的 Firebase 身份验证失败:NotAuthorizedError
【发布时间】:2017-04-30 12:12:17
【问题描述】:

我正在尝试通过 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 未正确初始化,但我已多次检查链接和密钥。我做错了什么?

【问题讨论】:

  • 你有更详细的错误响应吗?
  • 不幸的是,我没有。这就是它给我的一切。
  • 你试过了吗?我的意思是我的回答。使用 Server Key 后是否可以发送消息?

标签: javascript node.js heroku firebase firebase-cloud-messaging


【解决方案1】:

在您的代码中,我注意到了这部分(特别是注释):

// Planet Firebase API Key
var API_KEY = "AIz..."; // found at Project Settings > General > Web API Key
var fcm = new FCM(API_KEY);

您可能会收到401 - Authentication Error

使用 FCM 时,您应该始终使用 服务器密钥,而不是 Web API 密钥。
这在 Firebase Console 中也可见: 项目设置 > 云消息传递 > 服务器密钥。

请参阅我的回答here,了解有关不同键的想法。

【讨论】:

  • 这似乎没有帮助。我仍然遇到身份验证错误。
  • ©user1072264 奇数。您可以尝试通过简单的 cURL 发送,看看问题是否仍然相同?
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 2017-03-16
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 2017-08-24
  • 2020-05-22
相关资源
最近更新 更多