Firebase消息传递V1 401项目不允许错误
我正在使用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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题在于,新的Firebase项目仅默认启用了新的“ Firebase Cloud Messaging API(V1)”,并且使用该配置 - 官方
firebase -admin
nodejs library-使用新的V1 API - 将无法发送消息,并且将获得401project_not_permitten
错误。为了能够从服务器发送消息,您还必须启用较旧的“遗产” API 。
注意:
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 401PROJECT_NOT_PERMITTED
errors.To be able to send message from your server, you MUST also enable the older "legacy" API.
Note:
事实证明,我禁用了此API:“ firebase应用程序内消息API”
事实证明,这个问题以前是在问的,但我找不到。 是答案
这
APIS和Services
启用API&amp;服务
+启用API和服务
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:
APIs and Services
Enabled APIs & Services
+ Enable APIs and Services
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.
我遇到了同样的问题,由于云消息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
因此,在您的情况下,这应该有效:
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:
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