我应该使用什么 Bearer 令牌进行 Firebase 云消息传递测试? [英] What Bearer token should I be using for Firebase Cloud Messaging testing?

查看:15
本文介绍了我应该使用什么 Bearer 令牌进行 Firebase 云消息传递测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Postman 使用 Firebase Cloud Messaging 发送测试通知.我正在对这个 url 做一个 POST

https://fcm.googleapis.com/v1/projects/[我的项目名称]/messages:send

Postman 中的 Authorization 选项卡设置为 No Auth,而我的 Headers 选项卡如下所示

内容类型:application/json授权:Bearer [服务器密钥]

[服务器密钥] 是我的 Firebase 项目设置"区域的云消息传递"选项卡中新生成的服务器密钥.我不断收到此错误作为响应.

错误":{代码":401,"message": "请求具有无效的身份验证凭据.需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据.请参阅 https://developers.google.com/identity/sign-in/web/devconsole-project.",状态":未经身份验证"}

根据我能找到的一切,我使用了正确的令牌,但 Google 似乎不同意.我应该发送什么作为 Authorization 标头来解决此错误?

解决方案

获取Authentication Bearer的步骤:

  1. 访问 Google OAuth Playground:

    如果您想使用 cURL,用于数据通知:

    curl --location --request POST 'https://fcm.googleapis.com/v1/projects/your-project-id/messages:send' --header '内容类型:应用程序/json' --header '授权:承载你的访问令牌-*****-wqewe' --data-raw '{消息":{令牌":设备令牌-qwfqwee-***-qefwe",数据":{Key1":val1",Key2":val2";}}}'

    I am trying to send a test notification using Firebase Cloud Messaging via Postman. I'm doing a POST to this url

    https://fcm.googleapis.com/v1/projects/[my project name]/messages:send
    

    The Authorization tab in Postman is set to No Auth and my Headers tab looks like this

    Content-Type: application/json
    Authorization: Bearer [server key]
    

    [server key] is a newly generated server key in the 'Cloud Messaging' tab of my Firebase project's 'Settings' area. I keep getting this error in response.

    "error": {
        "code": 401,
        "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
        "status": "UNAUTHENTICATED"
    }
    

    Based on everything I can find, I'm using the right token, but it seems Google disagrees. What should I be sending as the Authorization header to get past this error?

    解决方案

    Steps to get Authentication Bearer:

    1. Got to Google OAuth Playground: https://developers.google.com/oauthplayground
    2. In the "Input your own scopes" for FCM use this url: https://www.googleapis.com/auth/firebase.messaging
    3. Tap Authorize API.
    4. Pick correct user for authorisation and allow access.
    5. In the Step 2: Exchange authorization code for tokens tap Exchange authorisation code for tokens.
    6. Access token is your Bearer.

    Steps to send FCM throw Postman:

    1. URL to send: https://fcm.googleapis.com/v1/projects/projectid-34543/messages:send
    2. Request Type: POST
    3. Headers: Content-Type -> application/json & Authorization -> Bearer
    4. In the body section enter APS payload with the right device token.
    5. Click send.

    In case you want to use cURL, for a data-notification:

    curl --location --request POST 'https://fcm.googleapis.com/v1/projects/your-project-id/messages:send' 
    --header 'Content-Type: application/json' 
    --header 'Authorization: Bearer your-access-token-*****-wqewe' 
    --data-raw '{
        "message": {
            "token": "device-token-qwfqwee-***-qefwe",
            "data": {
                "Key1": "val1",
                "Key2": "val2"
            }
        }
    }'
    

    这篇关于我应该使用什么 Bearer 令牌进行 Firebase 云消息传递测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆