前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >windows 环境下使用 Node.js 访问 SAP OData 遇到 unable to get local issuer certificate

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

作者头像
Jerry Wang
发布2022-10-24 15:16:53
1.7K0
发布2022-10-24 15:16:53
举报

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

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

代码语言:javascript
复制
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.

在应用程序生成过程中,当用户连接的系统使用 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 状态码,说明与服务器的连接终于通了:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解决方案
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档