翻译windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate

苍和静阅读量 8

我有一个 OData 服务,部署在 SAP S/4HANA 服务器上,浏览器里通过如下 url,可以正常访问到其元数据:

![windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate_OData](https://s2.51cto.com/images/blog/202210/15152138_634a5f821edc03316.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)

我编写了一个 node.js 应用,通过编程的方式访问这个 OData 服务的元数据:

复制代码
var request = require('request');

var url = 'https://ldai2xxx:44356/sap/opu/odata/sap/ZBOOK_MANAGE_SRV/$metadata';

var oOptions = {
        url: url,
        method: 'GET'
    };

var oPromise = new Promise(function(resolve,reject){
    request.get(oOptions,function(error,response,body){
        if(error){
            console.log("error occurred: " + error);
            reject(error);
        }
        resolve(body);
    });
});

oPromise.then((data) => console.log(data));

使用 node 执行上面的代码,遇到如下错误消息:

error occurred: Error: unable to get local issuer certificate
(node:33376) UnhandledPromiseRejectionWarning: Error: unable to get local issuer certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1497:34)
at TLSSocket.emit (events.js:315:20)
at TLSSocket._finishInit (_tls_wrap.js:932:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:706:12)
(Use ​​​node --trace-warnings ...​​​ to show where the warning was created)
(node:33376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag ​​​--unhandled-rejections=strict​​​ (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:33376) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

![windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate_前端_02](https://s2.51cto.com/images/blog/202210/15152138_634a5f8272e698477.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)

在应用程序生成过程中,当用户连接的系统使用 SSL 支持安全 HTTPS 流量时,可能会出现无效安全证书错误。 在某些情况下,证书是使用用户操作系统未知的本地证书颁发机构生成的。 如果发生这种情况,应用程序生成器会拒绝连接请求并报告错误。

解决方案

windows 环境下设置环境变量,使用 set 语法,命名后不加空格,直接附上两个 ​​&&​​, 然后空格,跟上新的命令。

​set NODE_TLS_REJECT_UNAUTHORIZED=0&& node ag3.js​

这种解决方案的用意是,设置环境变量 NODE_TLS_REJECT_UNAUTHORIZED 的值为 0,暂时禁用 SSL certificate 的校验。

执行之后,看到警告消息:

Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.

提示我们,这个环境变量的设置,禁掉了 certificate 验证,让 TLS 连接和 HTTPS 请求变得不再安全。

收到 401 状态码,说明与服务器的连接终于通了:

![windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate_node.js_03](https://s2.51cto.com/images/blog/202210/15152138_634a5f828e61771767.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=/resize,m_fixed,w_1184)


复制代码
    ===========================
    【来源: 51CTO】
    【作者: JerryWang汪子熙】
    【原文链接】 https://blog.51cto.com/jerrywangsap/5759537
    声明:转载此文是出于传递更多信息之目的。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与本网联系,我们将及时更正、删除,谢谢。
标签: node.js
0/300
全部评论0
0/300