[英]Firebase Messaging v1 401 Project Not Permitted error
I am running a node server with the firebase admin sdk.我正在使用 firebase 管理员 sdk 运行节点服务器。 However, everytime I try to send a push notification from the server, I get a 401 error.
但是,每次我尝试从服务器发送推送通知时,都会收到 401 错误。
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.
我已经设置了我的服务帐户,并下载了 .json 文件。 I even went into the gcloud platform and tried to add any permission that looked correct.
我什至进入 gcloud 平台并尝试添加任何看起来正确的权限。 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.我不确定还能做什么,因为我已经多次查看 v1 文档,但仍然不知道我缺少什么权限。 I even made sure to "firebase login" into the correct google account to see if that could've been an issue.
我什至确保“firebase 登录”到正确的谷歌帐户,看看这是否可能是一个问题。
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.我已经验证了 .json 文件中的 client_id、client_email 和 private_key_id 值。 I haven't yet verified the private_key property because I'm not sure where to find it.
我还没有验证 private_key 属性,因为我不确定在哪里可以找到它。
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.原来我禁用了这个 API:“Firebase In-App Messaging API” 原来这个问题之前也有人问过,但我找不到。 Here's the answer
这是答案
If anyone else runs into this issue this was my fix:如果其他人遇到此问题,这是我的解决方法:
APIs and Services
APIs and Services
Enabled APIs & Services
Enabled APIs & Services
+ Enable APIs and Services
+ Enable APIs and Services
Firebase In-App Messaging API
and make sure it's enabled.Firebase In-App Messaging API
并确保它已启用。 You can also just search for messaging and make sure that all the cloud messaging APIs are enabled.您也可以只搜索消息传递并确保所有云消息传递 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 will not be able to send messages and will get 401 PROJECT_NOT_PERMITTED
errors.问题是新的 Firebase 项目默认启用了新的“Firebase Cloud Messaging API (V1)”,并且使用该配置 - 官方的
firebase-admin
NodeJS 库将无法发送消息并会收到 401 PROJECT_NOT_PERMITTED
错误。
To be able to send message from your server, you MUST also enable the older "legacy" API .为了能够从您的服务器发送消息,您还必须启用较旧的“旧版” API 。
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.这与@Tial的自我答案基本相同,但我对此感到困惑并且(如评论)它有一些错误 - 我花了一段时间来理解它(以及链接的答案)才能让它正确,所以我不得不澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.