Firebase消息传递V1 401项目不允许错误

发布于 2025-02-04 02:37:31 字数 1892 浏览 3 评论 0原文

我正在使用Firebase Admin SDK运行节点服务器。但是,每次我尝试从服务器发送推送通知时,我都会收到401错误。

这是我遇到的确切错误:

  errorInfo: {
code: 'messaging/authentication-error',
message: 'An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>\n' +
  '<HEAD>\n' +
  '<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n' +
  '</HEAD>\n' +
  '<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n' +
  '<H1>PROJECT_NOT_PERMITTED</H1>\n' +
  '<H2>Error 401</H2>\n' +
  '</BODY>\n' +
  '</HTML>\n' +
  '". Status code: 401.'
},
codePrefix: 'messaging'

我不确定为什么我没有该项目的权限。我已经设置了我的服务帐户,并下载了.json文件。我什至进入了GCLOUD平台,试图添加任何看起来正确的许可。以下是与我的服务帐户相关的所有权限:

“权限”

我在本地运行服务器,并以这样的初始化了该应用程序:

const admin = require('firebase-admin');
const messaging = require('firebase-admin/messaging');
const serviceAccount = require('<path-to-key>');

const fbApp = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: '<PROJECT_ID>',
    databaseURL: '<DB_URL>'
});

我不确定我多次浏览了V1文档时该怎么做,并且仍然没有任何关于我缺乏的权限的线索。我什至确保将“ firebase登录”到正确的Google帐户中,以查看这是否可能是一个问题。

这是我的代码发送消息:

const sendPushNotifications2 = async (topic, reminder) => {
    const payload = genPayload2(reminder);
    //await messaging.getMessaging(fbApp).sendToTopic(topic, payload);
    await admin.messaging(fbApp).sendToTopic(topic, payload);
};

我已经验证了.json文件中的client_id,client_email和private_key_id value。我尚未验证private_key属性,因为我不确定在哪里可以找到它。

I am running a node server with the firebase admin sdk. However, everytime I try to send a push notification from the server, I get a 401 error.

Here's the exact error I'm getting:

  errorInfo: {
code: 'messaging/authentication-error',
message: 'An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>\n' +
  '<HEAD>\n' +
  '<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n' +
  '</HEAD>\n' +
  '<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n' +
  '<H1>PROJECT_NOT_PERMITTED</H1>\n' +
  '<H2>Error 401</H2>\n' +
  '</BODY>\n' +
  '</HTML>\n' +
  '". Status code: 401.'
},
codePrefix: 'messaging'

I'm not exactly sure why I don't have permissions to the project. I have setup my service account, and downloaded the .json file. I even went into the gcloud platform and tried to add any permission that looked correct. Below are all the permissions associated with my service account:

permissions

I am running the server locally, and initialized the app like this:

const admin = require('firebase-admin');
const messaging = require('firebase-admin/messaging');
const serviceAccount = require('<path-to-key>');

const fbApp = admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: '<PROJECT_ID>',
    databaseURL: '<DB_URL>'
});

I am not sure what else to do as I've looked through the v1 documentation multiple times and still don't have any clue as to what permissions I'm lacking. I even made sure to "firebase login" into the correct google account to see if that could've been an issue.

Here's my code to send a message:

const sendPushNotifications2 = async (topic, reminder) => {
    const payload = genPayload2(reminder);
    //await messaging.getMessaging(fbApp).sendToTopic(topic, payload);
    await admin.messaging(fbApp).sendToTopic(topic, payload);
};

I have verified the client_id, client_email, and private_key_id values in the .json file. I haven't yet verified the private_key property because I'm not sure where to find it.

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

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

发布评论

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

评论(4

莫多说 2025-02-11 02:37:31

问题在于,新的Firebase项目仅默认启用了新的“ Firebase Cloud Messaging API(V1)”,并且使用该配置 - 官方firebase -admin nodejs library-使用新的V1 API - 将无法发送消息,并且将获得401 project_not_permitten错误。

为了能够从服务器发送消息,您还必须启用较旧的“遗产” API

  1. 从您的Firebase控制台中,转到项目设置并打开“云消息”选项卡。
  2. 确保启用了列出的两个API - 请参阅屏幕截图的外观:
  3. 如果未启用任何API,则为禁用的API单击右侧的三个点菜单,然后
  4. 在打开的Google Cloud Console页面中选择“管理API”,请单击“启用”按钮。

注意:

  • 这与 @tial的自我答案基本上是相同的答案,但是我对此感到困惑,并且(正如注释)它错误了 - 我花了一段时间才能理解它(以及链接的答案)才能使它正确,所以我不得不澄清。
  • 在本文时,无论您使用新的(V1)或纬度API,此问题都会影响云消息API的所有用途。我希望Google在某个时候解决此问题。

The problem is that new Firebase projects have only the new "Firebase Cloud Messaging API (V1)" enabled by default, and with that configuration - the official firebase-admin NodeJS library - that uses the new V1 API - will not be able to send messages and will get 401 PROJECT_NOT_PERMITTED errors.

To be able to send message from your server, you MUST also enable the older "legacy" API.

  1. From your Firebase console, go to Project Settings and open the Cloud Messaging tab.
  2. Make sure that both APIs listed are enabled - see screenshot of how it should look:
    enter image description here
  3. If any API is not enabled, for the API that is disabled click the three dots menu on the right and select "Manage API"
  4. In the Google Cloud Console page that opens, click the "Enable" button.

Note:

  • This is basically the same answer as @Tial's self answer, but I was confused by it and (as commented) it got some things wrong - it took me a while to understand it (and the linked answer) to get it right, so I had to clarify.
  • As of this writing this issue impacts all uses of the Cloud Messaging APIs, regardless if you use the new (V1) or the lagacy API. I expect Google to fix this at some point.
空名 2025-02-11 02:37:31

事实证明,我禁用了此API:“ firebase应用程序内消息API”
事实证明,这个问题以前是在问的,但我找不到。 是答案

  1. Google Cloud Platform网站
  2. 访问APIS和Services
  3. 转到启用API&amp;服务
  4. 单击+启用API和服务
  5. 搜索firebase In-App Messaging Messaging API并确保已启用它。

您还可以搜索消息传递,并确保启用所有云消息API。

It turns out that I had this API disabled: "Firebase In-App Messaging API"
It also turns out that this question was asked before, but I wasn't able to find it. Here's the answer

If anyone else runs into this issue this was my fix:

  1. go to the google cloud platform website
  2. go to APIs and Services
  3. go to Enabled APIs & Services
  4. click + Enable APIs and Services
  5. search for Firebase In-App Messaging API and make sure it's enabled.

You can also just search for messaging and make sure that all the cloud messaging APIs are enabled.

蒲公英的约定 2025-02-11 02:37:31

我遇到了同样的问题,由于云消息API(旧版)现在被弃用,因此我努力使Firebase Cloud Messaging Messaging API(V1)工作。

我很幸运地使用getMessaging(app).send()方法代替.sendTopic()或.sendTodeVice(),因为这些方法正在使用旧的API。
更多信息:“ noreferrer”> https> https:// firbase。 google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.msesgaging.md#messaging_class_class

因此,在您的情况下,这应该有效:

import admin from 'firebase-admin';
import { initializeApp } from 'firebase-admin/app'; // I recommend you use initializeApp instead because admin is legacy
import { getMessaging } from 'firebase-admin/messaging';
import serviceAccount from '<path-to-key>';

const fbApp = initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: '<PROJECT_ID>',
    databaseURL: '<DB_URL>'
});

const messaging = getMessaging(fbApp);

const sendPushNotifications2 = async (topic, reminder) => {
    // message is defined as [BaseMessage][1] extended by [topic][2] or [token][3] or [condition][4]
    const message = {
        ...reminder, // suppose reminder is BaseMessage
        topic: topic
    };
    
    await messaging.send(message);
};

I had the same issue and since Cloud Messaging API (Legacy) is now deprecated I tried hard to get Firebase Cloud Messaging API (V1) to work.

I got lucky with using getMessaging(app).send() method instead .sendToTopic() or .sendToDevice() because those are using legacy api.
More here: https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messaging_class

So in your case, this should work:

import admin from 'firebase-admin';
import { initializeApp } from 'firebase-admin/app'; // I recommend you use initializeApp instead because admin is legacy
import { getMessaging } from 'firebase-admin/messaging';
import serviceAccount from '<path-to-key>';

const fbApp = initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: '<PROJECT_ID>',
    databaseURL: '<DB_URL>'
});

const messaging = getMessaging(fbApp);

const sendPushNotifications2 = async (topic, reminder) => {
    // message is defined as [BaseMessage][1] extended by [topic][2] or [token][3] or [condition][4]
    const message = {
        ...reminder, // suppose reminder is BaseMessage
        topic: topic
    };
    
    await messaging.send(message);
};
默嘫て 2025-02-11 02:37:31

2024年6月

删除了Firebase:云消息API(遗产)。
you must use Firebase Cloud Messaging API (V1)

you have to generate new key from here :

In june, 2024

Firebase removed : Cloud Messaging API (Legacy).
you must use Firebase Cloud Messaging API (V1)

you have to generate new key from here : generate from here

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