[英]Which token is needed to send push notifications through POST
I'm trying to set up a REST API server that could send push notifications using Firebase.我正在尝试设置可以使用 Firebase 发送推送通知的 REST API 服务器。 Google documentation says this is a valid POST request, as a start:
谷歌文档说这是一个有效的 POST 请求,作为开始:
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
However, I can't figure out which Authorization bearer is expected here.但是,我无法弄清楚这里应该使用哪个授权承载。 There is a ton of them on the firebase console, and I've tried each and every one of them, none work.
firebase 控制台上有很多,我已经尝试了每一个,都没有工作。 They all result in 401 response.
它们都导致 401 响应。
Where can I find the expected bearer for this request?我在哪里可以找到此请求的预期承载?
Thanks !谢谢 !
I find it easier to use legacy protocols to send push notifications.我发现使用旧协议发送推送通知更容易。 We just need the Server key which can be found in the Firebase Console.
我们只需要可以在 Firebase 控制台中找到的服务器密钥。
Firebase Console > Project Settings > Cloud Messaging > Server Key Firebase 控制台 > 项目设置 > 云消息传递 > 服务器密钥
HTTP request looks like this: HTTP 请求如下所示:
curl -X POST -H "Authorization: key=<your_server_key>" -H "Content-Type: application/json" -d '{
"notification": {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
},
"to": "/topics/<topic_name>"
}' "https://fcm.googleapis.com/fcm/send"
Check Server Reference for other APIs for server implementation.检查服务器参考以获取其他用于服务器实现的 API。
So, first of all go to Google Cloud Console and choose APIs&Services->Credentials.因此,首先 go 到Google Cloud Console并选择 APIs&Services->Credentials。 From there click on +Create Credentials->OAuth Client ID (Interface changes all the time so try to find this option).
从那里点击+Create Credentials->OAuth Client ID (界面一直在变化,所以试着找到这个选项)。 And then, simply choose Add Key and it will give you a json file.
然后,只需选择添加密钥,它就会为您提供 json 文件。
After you receive your json file you can try what was given in this guide: https://firebase.google.com/docs/cloud-messaging/auth-server .收到 json 文件后,您可以尝试本指南中给出的内容: https://firebase.google.com/docs/cloud-messaging/auth-server 。
If you want to send request from Postman then you can try https://developers.google.com/oauthplayground .如果您想从Postman发送请求,那么您可以尝试https://developers.google.com/oauthplayground 。 Register there with your google account and from available services select Firebase Cloud Messaging API v1 -> https://www.googleapis.com/auth/firebase.messaging .
Register there with your google account and from available services select Firebase Cloud Messaging API v1 -> https://www.googleapis.com/auth/firebase.messaging . There in the second step press Exchange authorization code for tokens.
在第二步中,按 Exchange 授权码获取令牌。 Finally, you will get your Access and Refresh Token there
最后,您将在那里获得访问和刷新令牌
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.