从节点JS

时间:2018-05-15 16:12:29

标签: node.js hmac

我正在尝试对需要签名的REST端点执行POST。请找到下面的代码段

var express = require("express");
var bodyParser = require("body-parser");
var crypto = require('crypto');
var request = require('request');


const APPLICATION_JSON_UTF8 = 'application/json; charset=utf-8';
const SIGNATURE = 'X-Hub-Signature';
const CONTENT_TYPE = 'Content-Type';
const botConfig = {
    webhookURL: 'https://8c9a9e46.ngrok.io/myURL',
    secretKey: 'SecretKey'
};
var app = express();

app.listen(3000, function () {
    console.log("app running on port.");
});

function buildSignatureHeader(buf, secret) {
    return 'sha256=' + buildSignature(buf, secret);
}
function buildSignature(buf, secret) {
    const hmac = crypto.createHmac('sha256', Buffer.from(secret, 'utf8'));
    hmac.update(buf);
    return hmac.digest('hex');
}
app.get('/', function (req, res) {
    const recvMessage = {
                        "userId":"2211333",
                        "messagePayload": {
                            "type":"Pony",
                            "text": "Hi There",
                            "channelName":"channelName"
                        },
                        "profile": {"firstName": 'bibin'}
                      };

 const data = Buffer.from(JSON.stringify(recvMessage), 'utf8');
 const headers = {};
    headers[CONTENT_TYPE] = APPLICATION_JSON_UTF8;
    headers[SIGNATURE]    = buildSignatureHeader(data, botConfig.secretKey);
    var interactive = false;
    var oauth=false;
   console.log(buildSignatureHeader(data, botConfig.secretKey));
   request.post({
        uri: botConfig.webhookURL,
        headers: headers,
        body: data

    },function(error, response, body){
  console.log(body);
 console.log('error'+error);
 console.log('response'+response);
  });

});

现在,当我点击节点JS服务器时,我收到一个异常,说“#34;无法验证第一个证书"”。我已尝试在npm全局级别设置代理,但问题仍然存在。有没有人见过这个问题?有人可以给我一些指示吗?

0 个答案:

没有答案