【问题标题】:Axios response Error: certificate has expiredaxios响应报错:证书已过期
【发布时间】:2019-11-06 06:33:34
【问题描述】:

我正在使用 axios 向diro 发送请求以创建一个端点为/user/create 的用户。

但是,我不断收到这样的错误:

Error response: { Error: certificate has expired
    at TLSSocket.onConnectSecure (_tls_wrap.js:1055:34)
    at TLSSocket.emit (events.js:198:13)
    at TLSSocket.EventEmitter.emit (domain.js:448:20)
    at TLSSocket._finishInit (_tls_wrap.js:633:8)

这是我的要求:

const DIRO_API_KEY = ******
createUserToDiro = ({
  phone,
  first_name,
  last_name,
  birth_date,
  mcc_code
}) => {
  const mobile = phone;
  const firstname = first_name;
  const lastname = last_name;
  const dob = formatDiroDob(birth_date);
  const mcc = mcc_code;
  axios.post('https://api.dirolabs.com/user/create'), {
    firstname,
    lastname,
    dob,
    mobile,
    mcc,
    apikey: DIRO_API_KEY
  })
  .then(rs => console.log('Success response:', rs))
  .catch(err => console.log('Error response:',err));

};

是什么导致了这个问题,有什么办法可以解决吗?

【问题讨论】:

  • api端证书过期。我认为您应该联系 diro 的技术支持或等到问题得到解决。

标签: javascript node.js post axios response


【解决方案1】:

axios库错误明确提到证书已过期。

请让 diro 更新 SSL 证书。

另一种我们可以跳过在 Axios npm 库中检查 SSL 的方法,如下所示

const axios = require('axios');
const https = require('https');

// At request level
const agent = new https.Agent({
    rejectUnauthorized: false
});

// Make a request for a user
axios.get('/user/create', { httpsAgent: agent })
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .finally(function () {
    // always executed
  });

这行得通。

【讨论】:

  • 我在使用 Axios 进行一些服务调用的 VSCode 扩展中遇到了这个问题。链中所有证书的证书仍然有效,并且没有一个过期,仍然只有使用扩展的 Windows 用户会收到此消息,而 Linux 和 Mac 用户不会收到此消息。因此,这似乎是 Axios 或 https IMO 中的一个错误。我目前的猜测是它与仅受目标端点支持的 TLS 1.3 相关
  • 我在Ubuntu 16.04 上遇到过这个问题。然而同样的Axios 请求在Ubuntu 20.04 下工作正常。如果不使用此处提供的解决方案,不确定如何在 Ubuntu 16.04 上修复它。
  • @W.M.事实证明,就我而言,Let's Encrypt 在去年稍微改变了他们的证书链,他们的根证书现在不是真正的根证书,而是指向旧的、过时的 AFAIK。 Axios 将使用默认信任库(如果存在)来执行 TLS 握手,在 Linux 上通过 OpenSSL 和 Mac 通过 LibreSSL 库通常可用于更新的信任链。然而,在 Windows 上,这样的默认 SSL 库通常不存在,因此无法正确验证整个信任链,并且在某些时候会使用旧的、过时的根证书。
【解决方案2】:

更短的方法,使用这个环境变量 NODE_TLS_REJECT_UNAUTHORIZED=0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2016-09-21
    • 2014-03-13
    • 2020-09-21
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多