'请求 https://www.googleapis.com/oauth2/v4/token 失败,原因:无法获取本地颁发者证书。节点JS

'request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate. NodeJS

节点版本 - v12.16.1 NPM 版本 - 6.13.4

我在 Nodejs 中使用以下代码,使用 google 云计算库从 google 云获取虚拟机列表。按照这个 link - https://github.com/googleapis/nodejs-compute#before-you-begin


// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application
const Compute = require('@google-cloud/compute');
rejectUnauthorized: false;//add when working with https sites
requestCert: false;//add when working with https sites
agent: false;//add when working with https sites
// Creates a client
const compute = new Compute();

async function getVmsExample() {
  // In this example we only want one VM per page
  const options = {
    maxResults: 1,
  };
  const vms = await compute.getVMs(options);
  return vms;

}

// Run the examples
exports.main = async () => {
  const vms = await getVmsExample().catch(console.error);
  if (vms) console.log('VMs:', vms);
  return vms;
};

if (module === require.main) {
  exports.main(console.log);
}

我已经满足了所有先决条件,但是我 运行 代码却出现以下错误-

FetchError: request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate
    at ClientRequest.<anonymous> (C:\Users\username\Desktop\Full-Stack\NodeJS\node-examples\node_modules\node-fetch\lib\index.js:1455:11)
    at ClientRequest.emit (events.js:311:20)
    at TLSSocket.socketErrorListener (_http_client.js:426:9)
    at TLSSocket.emit (events.js:311:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  message: 'request to https://www.googleapis.com/oauth2/v4/token failed, reason: unable to get local issuer certificate',
  type: 'system',
  errno: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
  code: 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
  config: {
    method: 'POST',
    url: 'https://www.googleapis.com/oauth2/v4/token',
    data: {
      grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      assertion: 'eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJub2RlanNhY2NvdW50QGNvZ2VudC1jYXNlLTI0MjAxNC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInNjb3BlIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9jb21wdXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vb2F1dGgyL3Y0L3Rva2VuIiwiZXhwIjoxNTg3OTU4NDcwLCJpYXQiOjE1ODc5NTQ4NzB9.QSn0bSjHtph4aHGZcXIkWhbbUxampHSOE1BsDkI8dZOah12ICHFOZV0zwrngCPbTMr4MIfTAE7s8fLESjCUEq7lPSvB0uTqU5Lr3fI4FUUEqOGp56821Lh68Z8stWmKb-9HV85h7Ub0aSkJdnezYMcK_-FPu__a3ZLeP3lEnjJu9292DtctGT73XvHaeDTMFiHSI10BlJ2LIPds5lC6XM5I4f6W-4UH0VhUgLo1uCGxJJj0jnkQZbjp11l8KSwsMuIMFvug8G6Y5OKP1E4Ef1EKoEBFGC-vjIjaCPiqkFv4U1yh8xc7ShXh2MBQ8eyUZY1OvDNO4IXexQ-RoWBt0pQ'
    },
    headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
    responseType: 'json',
    params: [Object: null prototype] {},
    paramsSerializer: [Function: paramsSerializer],
    body: '{"grant_type":"urn:ietf:params:oauth:grant-type:jwt-bearer","assertion":"eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJub2RlanNhY2NvdW50QGNvZ2VudC1jYXNlLTI0MjAxNC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInNjb3BlIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9jb21wdXRlIiwiYXVkIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vb2F1dGgyL3Y0L3Rva2VuIiwiZXhwIjoxNTg3OTU4NDcwLCJpYXQiOjE1ODc5NTQ4NzB9.QSn0bSjHtph4aHGZcXIkWhbbUxampHSOE1BsDkI8dZOah12ICHFOZV0zwrngCPbTMr4MIfTAE7s8fLESjCUEq7lPSvB0uTqU5Lr3fI4FUUEqOGp56821Lh68Z8stWmKb-9HV85h7Ub0aSkJdnezYMcK_-FPu__a3ZLeP3lEnjJu9292DtctGT73XvHaeDTMFiHSI10BlJ2LIPds5lC6XM5I4f6W-4UH0VhUgLo1uCGxJJj0jnkQZbjp11l8KSwsMuIMFvug8G6Y5OKP1E4Ef1EKoEBFGC-vjIjaCPiqkFv4U1yh8xc7ShXh2MBQ8eyUZY1OvDNO4IXexQ-RoWBt0pQ"}',    validateStatus: [Function: validateStatus]
  }
}

我也试过 npm config set strict-ssl false

有人知道哪里出了问题吗?

感谢帮助!

启动客户端时,您可以将 strictSSL 设置为 false(您已设置)或传入新的有效证书。

将 strictSSL 设置为 false(您已经这样做了)然后更新证书文件(您应该可以在此处导出它们 - https://baltimore-cybertrust-root.chain-demos.digicert.com/

此 link http://registry.npmjs.org/npm 可能会被您组织中的 IT 管理员阻止。 (请验证)

此外,您可以参考 this stack overflow case as reference 以安全的方式和一些可能有助于您获得解决方案方向的各种替代方案。