【问题标题】:Facebook Messenger API: Send Structured Message(Java)Facebook Messenger API:发送结构化消息(Java)
【发布时间】:2017-02-21 20:07:44
【问题描述】:

在使用基于官方文档here 的通用模板使用 Facebook Messenger 发送结构化消息。 '正在使用 Java 构造 JSON 对象。每当我将 JSON 发送到 Facebook 时,我都会收到响应“400-错误请求”I。我尝试使用在线工具进行比较,java 生成的 JSON 与文档中提供的 JSON 相比,除了变量名之外没有什么不同。无法理解我在构建 JSON 时哪里出错了。

从 Java 代码生成的 JSON..

        {
"message": {
    "attachment": {
        "payload": {
            "elements": [
                {
                    "buttons": [
                        {
                            "title": "show website",
                            "type": "web_url",
                            "url": "https://google.com"
                        },
                        {
                            "payload": "sample payload",
                            "title": "Hi There",
                            "type": "postback"
                        }
                    ],
                    "default_action": {
                        "fallback_url": "https://www.google.com/",
                        "messenger_extensions": true,
                        "type": "web_url",
                        "url": "https://www.google.com/",
                        "webview_height_ratio": "tall"
                    },
                    "image_url": "https://s3-ap-southeast-1.amazonaws.com/primary-4495.png",
                    "subtitle": "Sample Sub Title",
                    "title": "Sample Title"
                }
            ],
            "template_type": "generic"
        },
        "type": "template"
    }
},
"recipient": {
    "id": "988459377921053"
}

}

对应的Java代码..

         JSONObject root1 = new JSONObject();
        JSONObject c01 = new JSONObject();
        JSONObject c11 = new JSONObject();

        JSONObject attachment = new JSONObject();
        JSONObject payload = new JSONObject();
        JSONArray arrayButton= new JSONArray();
        JSONArray arrayelements= new JSONArray();
        JSONObject elementsObj = new JSONObject();
        JSONObject defaultAction = new JSONObject();

        JSONObject buttons1 = new JSONObject();
        JSONObject buttons2 = new JSONObject();

        root1.put("recipient", c01);
            c01.put("id", userId);

        root1.put("message", c11);
            c11.put("attachment", attachment);
                attachment.put("type", "template");
                attachment.put("payload", payload);
                    payload.put("template_type", "generic");
                    payload.put("elements", arrayelements);
                        arrayelements.put(elementsObj);
                            elementsObj.put("title", "Sample Title");
                            elementsObj.put("image_url", "https://s3-ap-southeast-1.amazonaws.com/primary-4495.png");
                            elementsObj.put("subtitle", "Sample Sub Title");
                            elementsObj.put("default_action", defaultAction);

                                defaultAction.put("type", "web_url");
                                defaultAction.put("url", "https://www.google.com/");
                                defaultAction.put("messenger_extensions", true);
                                defaultAction.put("webview_height_ratio", "tall");
                                defaultAction.put("fallback_url", "https://www.google.com/");



                                buttons1.put("type", "web_url");
                                buttons1.put("url", "https://google.com");
                                buttons1.put("title", "show website");
                            arrayButton.put(buttons1);


                                buttons2.put("type", "postback");
                                buttons2.put("title", "Hi There");
                                buttons2.put("payload", "sample payload");
                            arrayButton.put(buttons2);

                            elementsObj.put("buttons", arrayButton);

上面的json和官方文档中提供的样例对比可以看到,只有元素的顺序不同。在过去的 2 天里一直被这个问题困扰..请帮助..

【问题讨论】:

  • 我从未使用过 Messenger API,但是,您是否尝试过以相同的顺序/名称创建元素?也许 API 正在按照文档中提供的相同顺序等待名称……而且,您正在请求中发送 access_token?
  • 我试过了还是一样的错误
  • 所以,我建议您从您的回复中记录错误。我认为400 bad request 不是整个错误消息..可能您有更多关于可能对您有帮助的错误信息。此外,请尝试在发布之前将您的 root1 转换为 JSON,如 here 所述

标签: java json facebook facebook-graph-api facebook-messenger


【解决方案1】:

发现我的错误!!

每当我们尝试使用 messenger_extensions 属性时,我们都必须将域名列入白名单,否则 Messenger 平台将返回 400 错误。由于我不需要 messenger_extensions 属性,我删除了整个默认操作部分,现在 messenger API 返回 200 个代码。如果您想将您的域列入白名单,您可以点击以下链接。

https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting

【讨论】:

    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 2017-02-14
    • 2016-08-11
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2017-06-04
    相关资源
    最近更新 更多