我试图从jira服务器下载一个文件使用URL,但我得到一个错误。 如何在代码中包含证书进行验证?

错误:

Error: unable to verify the first certificate in nodejs

at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:929:36)
   
  at TLSSocket.emit (events.js:104:17)

at TLSSocket._finishInit (_tls_wrap.js:460:8)

我的Nodejs代码:

var https = require("https");
var fs = require('fs');
var options = {
    host: 'jira.example.com',
    path: '/secure/attachment/206906/update.xlsx'
};

https.get(options, function (http_res) {
    
    var data = "";

  
    http_res.on("data", function (chunk) {
       
        data += chunk;
    });

   
    http_res.on("end", function () {
      
        var file = fs.createWriteStream("file.xlsx");
        data.pipe(file);
      
    });
});

当前回答

这为我工作=>添加代理和'rejectUnauthorized'设置为false

const https = require('https'); //Add This const bindingGridData = async () => { const url = `your URL-Here`; const request = new Request(url, { method: 'GET', headers: new Headers({ Authorization: `Your Token If Any`, 'Content-Type': 'application/json', }), //Add The Below agent: new https.Agent({ rejectUnauthorized: false, }), }); return await fetch(request) .then((response: any) => { return response.json(); }) .then((response: any) => { console.log('response is', response); return response; }) .catch((err: any) => { console.log('This is Error', err); return; }); };

其他回答

我正在使用nodemailer npm模块。下面的代码解决了这个问题

     tls: {
     // do not fail on invalid certs
     rejectUnauthorized: false
     }

GoDaddy SSL CCertificate

我在尝试使用GoDaddy证书连接到我们的后端API服务器时遇到过这种情况,下面是我用来解决问题的代码。

var rootCas = require('ssl-root-cas/latest').create();

rootCas
  .addFile(path.join(__dirname, '../config/ssl/gd_bundle-g2-g1.crt'))
  ;

// will work with all https requests will all libraries (i.e. request.js)
require('https').globalAgent.options.ca = rootCas;

PS:

使用捆绑的证书,不要忘记安装npm install ssl-root-cas库

这实际上为我解决了这个问题,从https://www.npmjs.com/package/ssl-root-cas

// INCORRECT (but might still work)
var server = https.createServer({
  key: fs.readFileSync('privkey.pem', 'ascii'),
  cert: fs.readFileSync('cert.pem', 'ascii') // a PEM containing ONLY the SERVER certificate
});

// CORRECT (should always work)
var server = https.createServer({
  key: fs.readFileSync('privkey.pem', 'ascii'),
  cert: fs.readFileSync('fullchain.pem', 'ascii') // a PEM containing the SERVER and ALL INTERMEDIATES
});

另一个肮脏的黑客,这将使你所有的请求不安全:

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0

这为我工作=>添加代理和'rejectUnauthorized'设置为false

const https = require('https'); //Add This const bindingGridData = async () => { const url = `your URL-Here`; const request = new Request(url, { method: 'GET', headers: new Headers({ Authorization: `Your Token If Any`, 'Content-Type': 'application/json', }), //Add The Below agent: new https.Agent({ rejectUnauthorized: false, }), }); return await fetch(request) .then((response: any) => { return response.json(); }) .then((response: any) => { console.log('response is', response); return response; }) .catch((err: any) => { console.log('This is Error', err); return; }); };