【问题标题】:Grails: JSON.parse() Error on Linux - Works on Windows - stumpedGrails:Linux 上的 JSON.parse() 错误 - 适用于 Windows - 难倒
【发布时间】:2011-12-22 14:24:41
【问题描述】:

我正在编写一些 Facebook OAuth 2.0 代码来修复当前插件。我已经完成了 99%,但是当我将代码部署到 Linux 时它不起作用(在 Windows 上开发)。

我得到的错误是:
Exception Message: Expected a ',' or '}' at character 277 of {"algorithm":"HMAC-SHA256","code":"AQDZPCNUY2VHnN1R340mArRUggpP1393V9E0VeL-fJbp0VdRBOzR5S5sHQB5ysdhfX8BrPP0u43EVi5OBx5MnrbxROJsuwMrqHmEDRa2pFm-GQ-2_R1blo_uBgOoKFG_Gm0TvtiKhzR1LwV8KNJlAgfCikuzx-zCxDmUz4t6b6xWbvrLPBMYZaLzdy5te3lYGqk","issued_at":1324533514,"user_id":"123144996"} Caused by: Expected a ',' or '}' at character 277 of {"algorithm":"HMAC-SHA256","code":"AQDZPCNUY2VHnN1R340mArRUggpP1393V9E0VeL-fJbp0VdRBOzR5S5sHQB5ysdhfX8BrPP0u43EVi5OBx5MnrbxROJsuwMrqHmEDRa2pFm-GQ-2_R1blo_uBgOoKFG_Gm0TvtiKhzR1LwV8KNJlAgfCikuzx-zCxDmUz4t6b6xWbvrLPBMYZaLzdy5te3lYGqk","issued_at":1324533514,"user_id":"123144996"}

就在这个错误之前。我收到了同样的消息,但没有结尾} 作为一个黑客,我实际上使用了如下代码:

if (!jsonData.trim().endsWith("}")) {
    jsonData = jsonData + "}"
}

我不确定这段代码在 Windows 和 Linux 上的工作方式有何不同。寻找想法。不知道是不是编码?新的 URL().text? decodeBase64 代码?想法?

    String[] signedRequestParts = signedRequest.split('\\.')
    // signedRequest gets deconstructed into a JSON packet
    String jsonData = new String(Base64.decodeBase64(signedRequestParts[1].getBytes()), 'UTF-8')
    // attempt at temporary hack fix    
    if (!jsonData.trim().endsWith("}")) {
        jsonData = jsonData + "}"
    }

    def json = JSON.parse(jsonData) // I get that error on this line
    def fbToken
    if (json.code) {
        def u = 'https://graph.facebook.com/oauth/access_token' +
                '?client_id=' + applicationId + '&client_secret=' + secret +
                '&redirect_uri=' + '&code=' + json.code
        def content = new URL(u).text
        fbToken = content.split("&")[0].split("=")[1];
    }

【问题讨论】:

  • 次要,问题是 Base64.decodeBase64 在此调用返回时缺少最后一个字符 }。

标签: java facebook json grails


【解决方案1】:

java.lang.String.getBytes() 也需要一个字符集。您是否尝试过 getBytes("UTF-8")?

【讨论】:

  • 是的,你的 'UTF-8' 被传递给 new String() 而不是 .getBytes()。
  • 我尝试使用 Base64 base64 = new Base64(true);在我解码之前。它适用于 Windows,但不适用于我的 Linux 服务器。我得到一个找不到匹配的构造函数:org.apache.commons.codec.binary.Base64(java.lang.Boolean)。有什么想法吗?
  • 不要使用 apache commons Base64,而是尝试使用 Grails Base64 编解码器:signedRequestParts[1].decodeAsBase64()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多