Facebook图形API批量请求的POST方法

3

我正在尝试使用axios发送graph api批处理请求。当我在postman中尝试时,我尝试了以下内容 - 我正在尝试通过批处理请求发送send 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。我已在正文中提供了收件人。

1个回答

1

您不需要对整个请求主体进行URL编码,只需对每个参数的内容进行编码即可。请将以下内容用作请求主体:

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

注意:我没有使用URL编码,而是转义了字符。这样代码更易读。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接