【问题标题】:How do I pass SSL Context into a Python Client that Uses the Autogenerated OpenAPI Python Client Library如何将 SSL 上下文传递到使用自动生成的 OpenAPI Python 客户端库的 Python 客户端
【发布时间】:2022-08-17 00:26:19
【问题描述】:

我有一个 Python3 OpenAPI 客户端应用程序,它给了我以下错误:“无法获取本地颁发者证书”。

这个应用程序使用OpenAPI generator 提供的自动生成的python 客户端库,它连接到我的HTTPS Node Express 服务(存根也是由OpenAPI 生成器生成的)。

我正在使用 5.4.0 版的 OpenAPI 生成器

我使用以下代码 sn-p 复制了错误:

from urllib import request
resp = request.urlopen(\"https://sdbie-sargrad.chgme.com:8081/api-docs\") 
html = resp.read()

然后我通过对这个简单的代码 sn-p 进行以下更新来修复错误:

from urllib import request
import ssl
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_REQUIRED 
context.load_verify_locations(\"/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem\")
resp = request.urlopen(\"https://sdbie-sargrad.chgme.com:8081/api-docs\", context=context) 
html = resp.read()

我的 OpenAPI 代码如下所示:

from api.vlc_api_1_5_0.openapi_client.model.video import Video
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiException
from api.vlc_api_1_5_0.openapi_client.exceptions import ApiTypeError
from api.vlc_api_1_5_0.openapi_client.configuration import Configuration
from api.vlc_api_1_5_0.openapi_client.api import video_api
from api.vlc_api_1_5_0.openapi_client.api_client import ApiClient

self.configuration = Configuration(host)
with ApiClient(self.configuration) as api_client:
    vid_api = video_api.VideoApi(api_client)
    try:
        inv = vid_api.get_video_inventory()
    except ApiException as e:
        self.logger.error(\"Exception when calling VideoApi->get_video_inventory: %s\\n\" % e)

顺便说一句,\"petstore api\" 看起来很像您在上面看到的我使用的 api 的结构。

如何将适当的 SSL 上下文(如上面的固定 sn-p 所示)传递到由 openapi 生成器自动生成的 \"VideoApi\" 的构造函数中?

    标签: python ssl openapi-generator


    【解决方案1】:

    因为看起来服务器可能正在使用自签名 SSL 证书,所以设置:

    configuration.verify_ssl = False

    应该做的伎俩

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2018-10-27
      • 2022-10-05
      • 2022-11-11
      • 2012-11-13
      • 2017-10-03
      • 2018-05-23
      相关资源
      最近更新 更多