![](/img/trans.png)
[英]Where to find Firebase server key, I have enabled Cloud Messaging API V1 but i can't find it
[英]Firebase Messaging v1 401 Project Not Permitted error
我正在使用 firebase 管理員 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 登錄”到正確的谷歌帳戶,看看這是否可能是一個問題。
這是我發送消息的代碼:
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 值。 我還沒有驗證 private_key 屬性,因為我不確定在哪里可以找到它。
原來我禁用了這個 API:“Firebase In-App Messaging API” 原來這個問題之前也有人問過,但我找不到。 這是答案
如果其他人遇到此問題,這是我的解決方法:
APIs and Services
Enabled APIs & Services
+ Enable APIs and Services
Firebase In-App Messaging API
並確保它已啟用。您也可以只搜索消息傳遞並確保所有雲消息傳遞 API 均已啟用。
問題是新的 Firebase 項目默認啟用了新的“Firebase Cloud Messaging API (V1)”,並且使用該配置 - 官方的firebase-admin
NodeJS 庫將無法發送消息並會收到 401 PROJECT_NOT_PERMITTED
錯誤。
為了能夠從您的服務器發送消息,您還必須啟用較舊的“舊版” API 。
這與@Tial的自我答案基本相同,但我對此感到困惑並且(如評論)它有一些錯誤 - 我花了一段時間來理解它(以及鏈接的答案)才能讓它正確,所以我不得不澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.