Nodejs https无法正常工作

时间:2016-06-08 05:11:23

标签: javascript node.js

我正在使用这个向facebook发送api请求:

var hash = crypto.createHmac('sha256', config.FACEBOOK_SECRET).update(access_token).digest('hex');
    console.log('hash = ' + hash);
var options = { //start of options
    hostname: 'graph.facebook.com',
    port: 443,
    //path: '/oauth/?appsecret_proof='+hash+'&access_token='+at,
    path: '/v2.6/me?fields=id,email,name&access_token=' + req.body.access_token + '&appsecret_proof=' + hash,
    method: 'GET'
}; //end of options



var fbResponse = [];

var req = https.request(options, (response) => {
      console.log('statusCode: ', response.statusCode);
      console.log('headers: ', response.headers);

      response.on('data', (d) => {
          console.log(d)
         fbResponse.push(d);
      });

      response.on('end', () => {
        console.log('No more data in response. ');

        res.send(JSON.stringify({
            r: fbResponse
        }));
        return;

        })
    });


    req.on('error', (e) => {
      console.error(e);
       res.status(500).send(JSON.stringify({
            r: 'error'
        }));
        return;
    });


req.end();

这在回复中返回这样的内容:

{"id":"123456","email":"bob\u0040gmail.com","name":"bobby bobski"}

请注意电子邮件没有“@”符号,而是具有\ u0040。我不想要这个,我想要@而不是。我不确定,因为我没有测试过这个,但我想这可能会发生在所有特殊字符上。我该怎么做才能得到真正的角色?

1 个答案:

答案 0 :(得分:-1)

我找到了答案。您需要解析响应然后进行字符串化。它会起作用。

console.log('This will now have the @ and not the unicode : ' + JSON.stringify(JSON.parse(fbResponse)) );
相关问题