首页 > 解决方案 > 从节点 JS 执行 POST 时无法验证第一个证书错误

问题描述

我正在尝试对需要签名的 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 服务器时,我收到一个异常,上面写着“无法验证第一个证书”。我尝试在 npm 全局级别设置代理,但问题仍然存在。有没有人看到这个问题?有人可以给我一些指示吗?

标签: node.jshmac

解决方案


推荐阅读