使用firebase admin SDK在node.js中发送消息时尝试使用FCM服务器进行身份验证的错误

发布于 2025-02-11 21:28:26 字数 945 浏览 0 评论 0原文

当我尝试发送以将FCM通知发送到某个主题时,我会收到以下错误...

错误:试图对FCM进行身份验证时发生了错误 服务器。确保用于身份验证此SDK具有的凭据 适当的许可。

我正在使用一个全新生成的服务帐户密钥,并正确指向它。我已经确认通往钥匙的路径是正确的。我还为该项目启用了云消息传递。

const { messaging } = require('firebase-admin');
var admin = require('firebase-admin');

console.log(process.cwd());

async function run() {
  try {
    var serviceAccount = require("/Users/myUser/src/my_project/node_admin/my_project-5617a-firebase-adminsdk-lwpk6-5dad9000e0.json");
    const topic = 'all';
  
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
    });
  
    const payload = {
      notification: {
          title: "Test1234",
          body: "body",
          sound: 'test-sound.wav'
      }
    };
  
    var options = {
        priority: "high"
    }
    
    await admin.messaging().sendToTopic(topic , payload , options);
  } catch (e) {
    console.log(e);
  }
}


run();

When I try to send to send an FCM notification to a topic I get the following error...

Error: An error occurred when trying to authenticate to the FCM
servers. Make sure the credential used to authenticate this SDK has
the proper permissions.

I am using a brand new generated service account key and pointing to it correctly. I have confirmed that the path to the key is correct. I have also enabled Cloud Messaging for the project.

const { messaging } = require('firebase-admin');
var admin = require('firebase-admin');

console.log(process.cwd());

async function run() {
  try {
    var serviceAccount = require("/Users/myUser/src/my_project/node_admin/my_project-5617a-firebase-adminsdk-lwpk6-5dad9000e0.json");
    const topic = 'all';
  
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
    });
  
    const payload = {
      notification: {
          title: "Test1234",
          body: "body",
          sound: 'test-sound.wav'
      }
    };
  
    var options = {
        priority: "high"
    }
    
    await admin.messaging().sendToTopic(topic , payload , options);
  } catch (e) {
    console.log(e);
  }
}


run();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2025-02-18 21:28:26

您好,我是复制代码并运行 - >它有效
因此,我认为您可以检查文件auth json的路径

var serviceAccount = require("/Users/myUser/src/my_project/node_admin/my_project-5617a-firebase-adminsdk-lwpk6-5dad9000e0.json");

好运!

Hello i was copy code and run --> it working
so i think you can check path of file auth json

var serviceAccount = require("/Users/myUser/src/my_project/node_admin/my_project-5617a-firebase-adminsdk-lwpk6-5dad9000e0.json");

good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文