Facebook Graph API帖子错误地显示了一些消息字符

时间:2012-10-05 12:47:29

标签: facebook-graph-api grails urlencode

我正在尝试使用facebook图表api向用户墙发帖子。 https://graph.facebook.com/me/feed

å,ä或ö这样的Charchters会显示错误的符号,如 ...

我在请求中发送的所有相关参数(消息,名称,描述)中使用了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会以不同的方式处理它吗?

enter image description here

由于


更新

在发送JSON请求的主体之前,我将params调整为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中的结果(不工作):

enter image description here

2 个答案:

答案 0 :(得分:4)

escapeHtml是错误的方法 - 您可以看到您的消息现在如何显示HTML实体。您不想发布HTML代码,而是想发布纯文本。

  

å,ä或ö这样的Charchters会显示错误的符号,如 ...

这很可能是因为您的应用程序使用了错误的字符编码。

确保使用UTF-8。 (或者参数值转换为UTF-8,然后再将它们传递给API,如果前者不可能的话。)

编辑:

  

我尝试在

之前将参数转换为UTF-8      

14:49:13 DEBUG [FacebookService] body:消息:Tommy +%C3%80%C3%84%C4%87%C3%95,姓名:......

这些是网址编码的 UTF-8字符。 URL编码在这里是有成效的。

  

14:49:52 DEBUG [headers]&gt;&gt;内容类型:application / x-www-form-urlencoded;的字符集=窗口1252

你不应该如此公然地陷入API的面孔: - )

说真的,找出是什么让你的应用程序在请求标头中发送该charset - 并看看你如何将其更改为UTF-8(或完全保留charset部分)。

答案 1 :(得分:0)

事实证明,它已经成为了EncoderRegistry类 有一个charset字段,用于请求数据。它默认为 平台编码。您可以按如下方式将其设置为UTF-8:

def graph(path, params, method = GET, contentType = JSON) {
        def http = null

        http = new TrustAllHttpBuilder(ConfigurationHolder.config.facebook.graph.uri)

        http.encoderRegistry = new EncoderRegistry( charset: 'utf-8' )  

        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

            }
        }
    }

Answer reference