首页 > 解决方案 > 在 NodeJS 中使用证书和 RSA 密钥获取令牌时出错

问题描述

我正在尝试使用密钥、证书和客户端 ID 在 NodeJS 中获取 oauth 令牌。

相同的代码:

        var form =  {
            client_id : CLIENT_ID,
            grant_type: 'client_credentials',
        }
        var headers= {
            'content-type': 'application/x-www-form-urlencoded',
        }
        var option = {
            method: 'POST',
            url: CERTIFICATE_URL + "/oauth/token",
            headers,
            form,
            httpsAgent:new https.Agent({
                cert: CLIENT_CERTIFICATE.replace(/\\n/g, '\n'),
                key: CLIENT_CERTIFICATE_KEY.replace(/\\n/g, '\n')
              })
          };
       
        request(option, function(error, response, body) {
                var token = null;
                console.log(error);
                console.log(JSON.parse(body).access_token)
                if(!error && response.statusCode == 200) {
                    token = JSON.parse(body).access_token;
                } else {
                    console.log(error)
                    error = error | new Error(body);
                }
                cb(error, token);
            });

但出现以下错误:错误:写入 EPROTO 140062523283264:错误:14094410:SSL 例程:ssl3_read_bytes:sslv3 警报握手失败:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1544:SSL 警报号 40 2021 -10-13T23:46:56.23+0530 [APP/PROC/WEB/0] 在 WriteWrap.afterWrite [as oncomplete] (net.js:789:14) 输出错误:'EPROTO',代码:'EPROTO',系统调用: '写'

证书和密钥是从云环境本身生成和获取的。这是基于 x509 MTLS 的身份验证。

标签: node.jsssloauthx509certificatemtls

解决方案


能够使用 axios 库来做到这一点,请求库似乎已被弃用。


推荐阅读