我正在尝试使用facebook graph api将消息发布到用户墙上。
https://graph.facebook.com/me/feed
å,ä或ö等字符会以����等符号错误显示。
我在请求中发送的所有相关事件参数(消息,名称,描述)周围都使用了apacheCommons StringEscapeUtils.encodeHtml。
def params = [
accessToken: getFacebookAccessToken(),
message: StringEscapeUtils.escapeHtml(deal.title),
pictureUrl: imageUrl,
name: StringEscapeUtils.escapeHtml(deal.title),
description: StringEscapeUtils.escapeHtml(deal.shortDescription),
actionLabel: StringEscapeUtils.escapeHtml(actionLabel),
actionUrl: actionUrl
]
def graph(path, params, method = GET, contentType = JSON) {
def http = null
http = new TrustAllHttpBuilder(ConfigurationHolder.config.facebook.graph.uri)
http.request( method, contentType) { req ->
uri.path = path
req.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES );
req.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout );
req.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeout );
requestContentType = URLENC
body = params
response.success = { resp, obj ->
return obj
}
}
}
更改后,“名称”和“描述”在Facebook中正确显示,但“消息”仍然无法正确呈现。
有什么想法为什么“消息”没有正确显示?
FB处理方式是否不同?
谢谢
更新资料
在发送JSON请求正文之前,我将参数调整为URLEncoded为UTF-8。
def params = [
accessToken: getFacebookAccessToken(),
message: URLEncoder.encode(deal.title,"UTF-8"),
pictureUrl: imageUrl,
name: URLEncoder.encode(deal.title,"UTF-8"),
description: URLEncoder.encode(deal.shortDescription,"UTF-8"),
actionLabel: URLEncoder.encode(actionLabel,"UTF-8"),
actionUrl: actionUrl
]
要求:
14:49:13 DEBUG [FacebookService] body: Message: Tommy+%C3%80%C3%84%C4%87%C3%95,Name: Tommy+%C3%80%C3%84%C4%87%C3%95,Description: %C3%80%C3%84%C4%87%C3%95%C3%80%C3%84%C4%87%C3%95,Caption: ,Action Label: TEST,ActionUrl: http://www.mytest.com
14:49:51 DEBUG [FacebookService] Calling facebook graph API https://graph.facebook.com/me/feed
14:49:51 DEBUG [TrustAllHttpBuilder] POST https://graph.facebook.com/me/feed
14:49:52 DEBUG [headers] >> POST /me/feed HTTP/1.1
14:49:52 DEBUG [headers] >> Accept: application/json, application/javascript, text/javascript
14:49:52 DEBUG [headers] >> Content-Length: 475
14:49:52 DEBUG [headers] >> Content-Type: application/x-www-form-urlencoded; charset=windows-1252
14:49:52 DEBUG [headers] >> Host: graph.facebook.com
14:49:52 DEBUG [headers] >> Connection: Keep-Alive
14:49:53 DEBUG [headers] << HTTP/1.1 200 OK
14:49:53 DEBUG [headers] << Access-Control-Allow-Origin: *
14:49:53 DEBUG [headers] << Cache-Control: private, no-cache, no-store, must-revalidate
14:49:53 DEBUG [headers] << Content-Type: application/json
14:49:53 DEBUG [headers] << Expires: Sat, 01 Jan 2000 00:00:00 GMT
14:49:53 DEBUG [headers] << Pragma: no-cache
14:49:53 DEBUG [headers] << X-FB-Rev:
14:49:53 DEBUG [headers] << X-FB-Debug:
14:49:53 DEBUG [headers] << Date: Fri, 05 Oct 2012 13:49:52 GMT
14:49:53 DEBUG [headers] << Connection: keep-alive
14:49:53 DEBUG [headers] << Content-Length: 40
FB中的结果(不起作用):
最佳答案
escapeHtml
是错误的方法–您将看到消息现在如何显示HTML实体。您不想发布HTML代码,而是想发布纯文本。
Charchters like å, ä or ö would display incorrectly with symbols like ���...
这很可能是因为您的应用程序使用了错误的字符编码。
确保使用UTF-8。 (或者,在无法将参数值传递给API之前,请将其转换为UTF-8)。
编辑:
I tried converting the params to UTF-8 before
14:49:13 DEBUG [FacebookService] body: Message: Tommy+%C3%80%C3%84%C4%87%C3%95,Name: …
这些是URL编码的UTF-8字符。 URL编码在这里适得其反。
14:49:52 DEBUG [headers] >> Content-Type: application/x-www-form-urlencoded; charset=windows-1252
您不要如此公然地躺在API的脸上:-)
认真地,找出使您的应用程序在请求 header 中发送该字符集的原因–以及如何将其更改为UTF-8(或完全保留字符集部分)。
关于facebook-graph-api - Facebook Graph API帖子错误地显示了一些消息字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746583/