Node.js JSON解析错误 [英] Node.js JSON parsing error

查看:338
本文介绍了Node.js JSON解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用node.js制作一个Facebook应用程序,但是我在检查签名请求时遇到麻烦。每次我提出请求时,程序都会抛出一个 SyntaxError:Unexpected token ILLEGAL

 未定义:1 
:721599476}
^^
语法错误:意外的标记ILLEGAL

罪魁祸首的功能如下:

  function parse_signed_request(signed_request,secret){
encoded_data = signed_request.split('。',2);
//解码数据
sig = encoded_data [0];
json = base64url.decode(encoded_data [1]);
data = JSON.parse(json); // ERROR在这里发生!

//检查算法 - 与错误无关
if(!data.algorithm || data.algorithm。 toUpperCase()!='HMAC-SHA256'){
console.error('未知算法,预期HMAC-SHA256');
返回null;
}

//检查sig - 与错误无关
expected_sig = crypto.createHmac('sha256',secret).update(encoded_data [ 。1])消化(的base64’ )代替(/ \ + /克, - )代替(/ \ //克, _)代替( =,)。;。
if(sig!== expected_sig){
console.error('Bad signed JSON Signature!');
返回null;
}

返回数据;
}

只是为了测试,一个有效的signed_request将是



'pre> WGvK-mUKB_Utg0l8gSPvf6smzacp46977pTtcRx0puE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyOTI4MjEyMDAsImlzc3VlZF9hdCI6MTI5MjgxNDgyMCwib2F1dGhfdG9rZW4iOiIxNTI1NDk2ODQ3NzczMDJ8Mi5ZV2NxV2k2T0k0U0h4Y2JwTWJRaDdBX18uMzYwMC4xMjkyODIxMjAwLTcyMTU5OTQ3NnxQaDRmb2t6S1IyamozQWlxVldqNXp2cTBmeFEiLCJ1c2VyIjp7ImxvY2FsZSI6ImVuX0dCIiwiY291bnRyeSI6ImF1In0sInVzZXJfaWQiOiI3MjE1OTk0NzYifQ

为什么在有效的JSON时收到此错误,只需使用JSON的静态字符串即可正常工作,并且有任何技巧可以解决此问题吗?



谢谢。

解决方案

好的,经过一番测试,我自己解决了这个问题,对于浪费的问题抱歉。

我的base64库中的某些东西没有正确地解码字符串(虽然它似乎是 - 所以它一定是一个非显示的字符cter或padding等)



我已经转换为 https://github.com/kriszyp/commonjs-utils/blob/master/lib/base64.js 适合我的目的,虽然需要修改支持base64url解码而不是正常的base64,现在似乎工作正常。


I am attempting to make a Facebook application with node.js, however I'm having trouble in checking signed requests. Every time I make a request, the program throws a SyntaxError: Unexpected token ILLEGAL as such:

undefined:1
":"721599476"}
              ^^
SyntaxError: Unexpected token ILLEGAL

The culprit function is below:

function parse_signed_request(signed_request, secret) {
    encoded_data = signed_request.split('.',2);
    // decode the data
    sig = encoded_data[0];
    json = base64url.decode(encoded_data[1]);
    data = JSON.parse(json); // ERROR Occurs Here!

    // check algorithm - not relevant to error
    if (!data.algorithm || data.algorithm.toUpperCase() != 'HMAC-SHA256') {
        console.error('Unknown algorithm. Expected HMAC-SHA256');
        return null;
    }

    // check sig - not relevant to error
    expected_sig = crypto.createHmac('sha256',secret).update(encoded_data[1]).digest('base64').replace(/\+/g,'-').replace(/\//g,'_').replace('=','');
    if (sig !== expected_sig) {
        console.error('Bad signed JSON Signature!');
        return null;
    }

    return data;
}

Just for testing, a valid signed_request would be

WGvK-mUKB_Utg0l8gSPvf6smzacp46977pTtcRx0puE.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyOTI4MjEyMDAsImlzc3VlZF9hdCI6MTI5MjgxNDgyMCwib2F1dGhfdG9rZW4iOiIxNTI1NDk2ODQ3NzczMDJ8Mi5ZV2NxV2k2T0k0U0h4Y2JwTWJRaDdBX18uMzYwMC4xMjkyODIxMjAwLTcyMTU5OTQ3NnxQaDRmb2t6S1IyamozQWlxVldqNXp2cTBmeFEiLCJ1c2VyIjp7ImxvY2FsZSI6ImVuX0dCIiwiY291bnRyeSI6ImF1In0sInVzZXJfaWQiOiI3MjE1OTk0NzYifQ

Why am I getting this error when it is valid JSON and simply using a static string of JSON will work fine, and are there any tips to fix this?

Thanks.

解决方案

Ok, after a bit of testing I've fixed the problem myself, sorry for the wasted question.

Something in my base64 library wasn't decoding the string properly (although it appeared to be - so it must have been a non-displaying character or padding, etc.)

I've changed over to https://github.com/kriszyp/commonjs-utils/blob/master/lib/base64.js which suits my purposes, although needed to be modified to support base64url decoding rather than normal base64, and it seems to work fine now.

这篇关于Node.js JSON解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆