Firebase云消息传递HTTP v1发送错误-400客户端错误:网址请求错误:https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send [英] Firebase cloud messaging HTTP v1 send error - 400 Client Error: Bad Request for url: https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send

查看:390
本文介绍了Firebase云消息传递HTTP v1发送错误-400客户端错误:网址请求错误:https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Firebase云消息传递HTTP v1

I try to use Firebase cloud messaging HTTP v1

这是在Windows下运行的Python代码

Here's the Python code which run under Windows

import json
import requests
from google.oauth2.service_account import Credentials
import google.auth.transport.requests

def _get_access_token():
    credentials = Credentials.from_service_account_file(
        'C:/yocto/wenote-notification/send_notification/wenote-206215-firebase-adminsdk-9fpls-a3fdd2934c.json',
        scopes=['https://www.googleapis.com/auth/firebase.messaging']
    )

    request = google.auth.transport.requests.Request()

    credentials.refresh(request)

    access_token = credentials.token

    return access_token

def generate_header():
    return {'Authorization' : 'Bearer ' + _get_access_token(), 'Content-type' : 'application/json'}


# Header    
headers = generate_header()
print(headers)


# Data
string = '{"message":{"token": "cSW75-6OF-w:APA91bE-E6vWucBe8rRijpDLlZGHhyfwoaLJr3R1TZEvieBM-__ZXwXlBS34kUticUN_eSbwvQGTymbmtd7sHT5U9O_v9HePyVn7jnUD9IBdoZZYSQ1CrgxXS1sz9wjAd5pKHIddoKj8", "data": {"sync": false, "sync_device_count": 2}, "fcm_options": {"analytics_label": "wenote_analytics_label"}}}'
request_data = json.loads(string)
print(request_data)


r = requests.post('https://fcm.googleapis.com/v1/projects/wenote-206215/messages:send', headers=headers, json=request_data)

if r.status_code != 200:
    r.raise_for_status()

我发送到Firebase服务器的邮件是

The message I send to Firebase server is

{
   "message":{
      "token":"cSW75-6OF-w:APA91bE-E6vWucBe8rRijpDLlZGHhyfwoaLJr3R1TZEvieBM-__ZXwXlBS34kUticUN_eSbwvQGTymbmtd7sHT5U9O_v9HePyVn7jnUD9IBdoZZYSQ1CrgxXS1sz9wjAd5pKHIddoKj8",
      "data":{
         "sync":false,
         "sync_device_count":2
      },
      "fcm_options":{
         "analytics_label":"wenote_analytics_label"
      }
   }
}

但是,出现以下错误

c:\yocto\wenote-notification\send_notification>python a.py
{'Authorization': 'Bearer ya29.c.Ko8BvQdCd-8US-DzQ-AcuTOURlGTQEvJt4mtofaeM08LRIhXBfC4RFN3QzkAcCdnaqI6uJLQlk1YJg67KQOhF8CtNux9t743nq2NN9uoW2mbQiB2y_2fjRUhiIIM7Wz2nt9rDOM4zFIFwKlHtLPXRLa4IGo0Ho-dq8zzVxQH1qPNGqy_ja8WowQCY5ReAQkmAmE', 'Content-type': 'application/json'}
{'message': {'token': 'cSW75-6OF-w:APA91bE-E6vWucBe8rRijpDLlZGHhyfwoaLJr3R1TZEvieBM-__ZXwXlBS34kUticUN_eSbwvQGTymbmtd7sHT5U9O_v9HePyVn7jnUD9IBdoZZYSQ1CrgxXS1sz9wjAd5pKHIddoKj8', 'data': {'sync': False, 'sync_device_count': 2}, 'fcm_options': {'analytics_label': 'wenote_analytics_label'}}}
Traceback (most recent call last):
  File "a.py", line 38, in <module>
    r.raise_for_status()
  File "C:\Python36\lib\site-packages\requests\models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://fcm.googleapis.com/v1/projects/wenote-206215/messages:send


有人知道为什么会这样吗,我该如何解决呢?


Any idea why it is so, and how I can resolve this?

我已经检查了Firebase云消息传递控制台.已启用.

I have check my Firebase cloud messaging console. It is enabled.

推荐答案

这是调试上述问题的方法.

This is the way to debug the above problem.

使用

if r.status_code != 200:
    print(r.text)
    r.raise_for_status()

代替

if r.status_code != 200:
    r.raise_for_status()

我们将收到以下错误

{
  "error": {
    "code": 400,
    "message": "Invalid value at 'message.data[0].value' (TYPE_STRING), false\nInvalid value at 'message.data[1].value' (TYPE_STRING), 2",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "field": "message.data[0].value",
            "description": "Invalid value at 'message.data[0].value' (TYPE_STRING), false"
          },
          {
            "field": "message.data[1].value",
            "description": "Invalid value at 'message.data[1].value' (TYPE_STRING), 2"
          }
        ]
      }
    ]
  }
}


HTTP v1看起来对值类型非常严格(必须为String)


Look like HTTP v1 is pretty strict on the value type (It need to be String)

因此,而不是使用

string = '{"message":{"token": "cSW75-6OF-w:APA91bE-E6vWucBe8rRijpDLlZGHhyfwoaLJr3R1TZEvieBM-__ZXwXlBS34kUticUN_eSbwvQGTymbmtd7sHT5U9O_v9HePyVn7jnUD9IBdoZZYSQ1CrgxXS1sz9wjAd5pKHIddoKj8", "data": {"sync": false, "sync_device_count": 2}, "fcm_options": {"analytics_label": "wenote_analytics_label"}}}'

我们需要使用

string = '{"message":{"token": "cSW75-6OF-w:APA91bE-E6vWucBe8rRijpDLlZGHhyfwoaLJr3R1TZEvieBM-__ZXwXlBS34kUticUN_eSbwvQGTymbmtd7sHT5U9O_v9HePyVn7jnUD9IBdoZZYSQ1CrgxXS1sz9wjAd5pKHIddoKj8", "data": {"sync": "false", "sync_device_count": "2"}, "fcm_options": {"analytics_label": "wenote_analytics_label"}}}'

这篇关于Firebase云消息传递HTTP v1发送错误-400客户端错误:网址请求错误:https://fcm.googleapis.com/v1/projects/&lt;PROJECT_ID&gt;/messages:send的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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