Skip to main content
 首页 » 编程设计

python之FCM API 'Bad request 400' 错误

2025年05月04日2252php

我正在尝试通过新的 FCM api 协议(protocol)实现发送通知。我通过了身份验证请求,我也在谷歌控制台中启用了 api。现在,当我发送请求时,我收到 Bad request 400 错误。响应不包含错误代码和错误消息以外的任何其他信息。甚至原因字段都显示“错误请求”。我正在用 FCM api docs 来实现它.

def fcm_test(): 
    FCM_URL = "https://fcm.googleapis.com/v1/projects/MY_PROJECT_NAME/messages:send" 
    my_token = "device-token" 
 
    payload = { 
        "massage": { 
            "token": my_token, 
            "notification": { 
                "body": "body", 
                "title": "title" 
            } 
        } 
    } 
 
    payload = json.dumps(payload) 
    headers = { 
        "Authorization": "Bearer {auth_token}".format(auth_token=_get_authentication_token()), 
        "Content-Type": "application/json; UTF-8", 
    } 
    try: 
        request = moves.urllib.request.Request(FCM_URL, payload.encode('utf-8'), headers) 
        response = moves.urllib.request.urlopen(request, timeout=10) 
    except Exception as e: 
        pass 
    return 

请您参考如下方法:

您的有效载荷 key 中有一个拼写错误,您可以用“message”而不是“massage”再试一次吗?