使用 post 方法的 Facebook Graph API BATCH 请求

Facebook Graph API BATCH request with post method

我正在尝试使用 axios 发送带有图形的批处理请求 api 批处理请求。 当我尝试 postman 我尝试了以下 - 我正在尝试 post 通过批量请求发送 api。

url = https://graph.facebook.com?access_token=EA...`.
raw body  = `{
    "batch":[
  {
    "method": "POST",
    "relative_url": "me/messages?access_token=<ACCESS_TOKEN>",
    "body": "{ \"messaging_type\": \"MESSAGE_TAG\",\r\n         \"tag\": \"ISSUE_RESOLUTION\",\r\n        \"recipient\": {\r\n          \"id\": \"1111944272264076\"\r\n        },\r\n        \"message\": {\r\n          \"text\": \"hello, world!\"\r\n        }}"
  }
]
}

响应是

[
    {
        "code": 400,
        "headers": [
            {
                "name": "WWW-Authenticate",
                "value": "OAuth \"Facebook Platform\" \"invalid_request\" \"(#100) The parameter recipient is required\""
            },
            {
                "name": "Expires",
                "value": "Sat, 01 Jan 2000 00:00:00 GMT"
            },
            {
                "name": "Strict-Transport-Security",
                "value": "max-age=15552000; preload"
            },
            {
                "name": "Facebook-API-Version",
                "value": "v2.10"
            },
            {
                "name": "Content-Type",
                "value": "text/javascript; charset=UTF-8"
            },
            {
                "name": "X-App-Usage",
                "value": "{\"call_count\":0,\"total_cputime\":0,\"total_time\":0}"
            },
            {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
            },
            {
                "name": "Cache-Control",
                "value": "no-store"
            },
            {
                "name": "Vary",
                "value": "Accept-Encoding"
            },
            {
                "name": "Pragma",
                "value": "no-cache"
            }
        ],
        "body": "{\"error\":{\"message\":\"(#100) The parameter recipient is required\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"BdrR5exbYCu\"}}"
    }
]

响应代码是200。我已经在正文中给出了收件人。

您不需要URL 对整个正文进行编码。只是每个参数的内容。试试这个作为请求的正文:

{
 "batch":[
  {
   "method":"POST",
   "relative_url":"me/messages", 
   "body": "recipient={\"id\": \"1111944272264076\"}&message={\"text\": \"hello, world!\"}"
  }
 ]
}

注意:我没有使用 URL 编码,而是对字符进行了转义。使代码更具可读性