【发布时间】:2017-02-19 00:40:14
【问题描述】:
我想显示我的代码,但我不知道为什么我的 Facebook 没有收到消息。非常感谢您的帮助
HttpURLConnection myConnection = (HttpURLConnection) ((new URL("https://graph.facebook.com/v2.6/me/messages?access_token=the_access_token").openConnection()));
myConnection.setDoOutput(true);
myConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
myConnection.setRequestProperty("Accept", "application/json");
myConnection.setRequestMethod("POST");
myConnection.connect();
JSONObject myTest = new JSONObject();
try {
JSONObject recipient = new JSONObject();
recipient.put("id", "martinxxxxxwang");
myTest.put("recipient", recipient);
JSONObject message = new JSONObject();
message.put("text", "888");
myTest.put("message", message);
response.getWriter().append(myTest.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStream os = myConnection.getOutputStream();
os.write(myTest.toString().getBytes("UTF-8"));
os.close();
顺便说一下,“myTest”的内容是{“recipient”:{“id”:“martinxxxxxwang”},“message”:{“text”:“888”}}。
如果我尝试
“graph.facebook.com/v2.6/me/messages?access_token=token”;,
我被告知 ""error": { "message": "(#100) The parameter user_id is required", "type": "OAuthException", "code": 100, "fbtrace_id": "FEv3jq/ahEq" } } "
【问题讨论】:
-
您是如何获得访问令牌的?您是否有权阅读来自此特定 FB 页面的消息?
-
@TimBiegeleisen:嗨,我在“/developers.facebook.com/apps/(my app)/messenger/”中获得的访问令牌,我试过“graph.facebook.com/v2.6/me/messages?access_token=token”,我被告知 ""error": { "message": "(#100) The parameter user_id is required", "type": "OAuthException", "code": 100, "fbtrace_id": "FEv3jq/ahEq" } } “所以我认为toekn没有问题。对于您的问题“您是否有权阅读来自此特定 FB 页面的消息?”我想是的,反正我不能很好地理解它。
-
我只偶尔使用过 Facebook Graph API,但我的建议是首先让调用在 Graph 浏览器中工作,然后再考虑你的 Java 代码。
标签: java json facebook httpurlconnection