简体   繁体   English

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

[英]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:如果其他人遇到此问题,这是我的解决方法:

  1. go to the google cloud platform website去谷歌云平台网站
  2. go to APIs and Services转到APIs and Services
  3. go to Enabled APIs & Services转到已Enabled APIs & Services
  4. click + Enable APIs and Services单击+ Enable APIs and Services
  5. search for 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

  1. From your Firebase console, go to Project Settings and open the Cloud Messaging tab.从您的 Firebase 控制台、go 到项目设置并打开云消息传递选项卡。
  2. Make sure that both APIs listed are enabled - see screenshot of how it should look:确保列出的两个 API 都已启用 - 请查看其外观的屏幕截图: 在此处输入图像描述
  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"如果任何 API 未启用,对于已禁用的 API,单击右侧的三点菜单和 select“管理 API”
  4. In the Google Cloud Console page that opens, click the "Enable" button.在打开的 Google Cloud Console 页面中,单击“启用”按钮。

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.这与@Tial的自我答案基本相同,但我对此感到困惑并且(如评论)它有一些错误 - 我花了一段时间来理解它(以及链接的答案)才能让它正确,所以我不得不澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在哪里可以找到 Firebase 服务器密钥,我已启用云消息传递 API V1 但我找不到它 - Where to find Firebase server key, I have enabled Cloud Messaging API V1 but i can't find it Android Studio:Firebase 云消息意外响应代码 401 - Android Studio : Firebase Cloud Messaging Unexpected response code 401 Firebase 消息传递错误:主题格式错误 - Firebase messaging error: Topic is malformed 在 Expo 项目中使用 Firebase In-App-Messaging - Use Firebase In-App-Messaging in Expo Project 在 React Native 的应用程序消息中安装 Firebase 时出错 - Error Installing Firebase In App Messaging in React Native Firebase 云消息令牌 - Firebase Cloud Messaging Token Firebase:firebase.messaging() 不是函数 - Firebase: firebase.messaging() is not a function 无法获取 Firebase 消息传递令牌。 [错误:[消息/未知]。 服务器请求太多。] - React Native IOS - Unable to get firebase messaging token. [Error: [messaging/unknown]. Too many server requests.] - React Native IOS Firebase 通知 admin.messaging() 不是 function - Firebase 云消息传递 - Firebase notification admin.messaging() is not a function - Firebase cloud messaging MismatchSenderId 在 Django - Firebase 云消息传递 - MismatchSenderId on Django - Firebase Cloud Messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM