javascript Node.js JSON 解析错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4486630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:51:26  来源:igfitidea点击:

Node.js JSON parsing error

javascriptjsonfacebooknode.jsparse-error

提问by Adam M-W

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 ILLEGALas such:

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

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

仅用于测试,有效的 signed_request 将是

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?

当它是有效的 JSON 时,为什么我会收到此错误并且仅使用静态 JSON 字符串就可以正常工作,是否有任何提示可以解决此问题?

Thanks.

谢谢。

采纳答案by Adam M-W

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.)

我的 base64 库中的某些内容没有正确解码字符串(尽管它看起来是 - 所以它一定是一个不显示的字符或填充等)

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

我已经改用https://github.com/kriszyp/commonjs-utils/blob/master/lib/base64.js这符合我的目的,虽然需要修改以支持 base64url 解码而不是普通的 base64,并且它现在似乎工作正常。