【问题标题】:Firebase Cloud Messaging on Web - send message via postmanWeb 上的 Firebase 云消息传递 - 通过邮递员发送消息
【发布时间】:2023-03-04 13:15:01
【问题描述】:

我正在尝试设置 Firebase Cloud Messaging for Web。我设法正确初始化它并获得令牌:

<script src="https://www.gstatic.com/firebasejs/4.13.0/firebase.js"></script>
<script>
    // Initialize Firebase
    var config = {
    apiKey: "xxx",
    authDomain: "name.firebaseapp.com",
    databaseURL: "https://name.firebaseio.com",
    projectId: "project-name",
    storageBucket: "project-name.appspot.com",
    messagingSenderId: "1111111111111"
    };
    firebase.initializeApp(config);

    const messaging = firebase.messaging();
    messaging.requestPermission().then(function() {
    })

    messaging.getToken().then(function(currentToken) {
        console.log("token: " + currentToken);
    })
</script>

+manifest.json 和 gcm_sender_id

我可以看到我正在控制台中获取令牌,因此我正在尝试验证它并通过 Postman - here is the documentation 发送我的第一个通知。

发布网址:https://fcm.googleapis.com/v1/projects/PROJECTID/messages:send 授权:无授权

标题

正文 - 令牌来自客户端函数 messaging.getToken().then(function(currentToken) {...

当我发布它时,我收到错误 401 UNAUTHENTICATED 并包含以下详细信息:

    "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"

有什么建议可以在邮递员中正确授权并进行测试吗?谢谢

【问题讨论】:

    标签: firebase oauth-2.0 firebase-cloud-messaging postman


    【解决方案1】:

    您需要为您的 firebase 项目生成访问令牌。示例here

    并在下面使用它

    curl -X POST -H "Authorization: Bearer {{access token value}}" -H 
    "Content-Type: application/json" 
    -d '{
        "message":{
         ....
        },
        "token": "{{device token}}"
        }
    }' "https://fcm.googleapis.com/v1/projects/{{project-name}}/messages:send"
    

    【讨论】:

    • 这似乎创建了一个带有私钥的 json 对象,这是一个多行的东西。我想我只想要一个简单的 jwt?
    猜你喜欢
    • 2020-03-15
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多