"unable to get local issuer certificate" on Windows with Python and Postman 添加客户端证书后

"unable to get local issuer certificate" on Windows with Python and Postman after adding the client certificate

我是 Windows 10.

的一名数据工程师

我正在尝试在 Python 中发出一个简单的 Post 请求,以检索自定义数据库服务的身份验证令牌。

我的代码尽可能简单:

import requests
import ssl
import certifi


url = ""
headers = {'Content_Type': 'application/x-www-form-urlencoded'}
payload = {
    'password': '',
    'scope': '',
    'client_id': '',
    'client_secret': '',
    'username': '',
    'grant_type': ''

}

if __name__ == '__main__':
    token_response = requests.request("POST", url, headers=headers, data=payload)
    print(token_response.content)

为了保护隐私,我删除了负载值和 url 值。当我 运行 代码时,我回来了 “url 超出最大重试次数”,以及“[SSL:CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1129)”。

我可以通过在我的 Post 请求中设置 verify=False 来避免这个错误,但是考虑到我正在处理敏感数据,我不能这样做。我提到这一点是为了证明我的凭据确实有效,而且 Cinchy 证书、我的 python 设置或我正在使用的业务网络(VPN?其他东西?)显然有问题。

我从这个答案 () 看出,可能是“如果您 运行ning 在业务网络下,请联系网络管理员在网络级别应用所需的配置。”我当然 运行 宁在业务网络下,所需的配置是什么样的?

关于此错误的大部分答案都提到您需要下载 url 的 CA 证书(例如此处 Windows: Python SSL certificate verify failed), but I have done that and it is not working. I went to the site, logged in, clicked on the Chrome icon with the lock, and downloaded the certificate as a Base-64 encoded X.509, and added it to my certifi cacert.pem file 'C:\Users\gblinick\AppData\Local\Programs\Python\Python39\lib\site-packages\certifi\cacert.pem' as per https://medium.com/@menakajain/export-download-ssl-certificate-from-server-site-url-bcfc41ea46a2.

如有任何帮助,我们将不胜感激。我的直觉是它与业务网络有关,但我不知道如何检查。想法会很棒。

如果我可以提供更多信息,请告诉我。

非常感谢!

编辑:Postman 的请求也不起作用,除非我关闭 SSL 身份验证,所以很明显这是同样的问题,这不仅仅是 Python 的问题。 但是,如果我使用 Powershell 发出相同的请求,它确实有效:

# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Result = Invoke-WebRequest -Uri $Endpoint -UseBasicParsing -Method 'Post' -Body $Body -Headers $Header -ErrorAction Stop # -SslProtocol Tls

无论我是否将第一行注释掉,这都有效。看起来 Powershell 可能有一些 Python 和 Powershell 没有的内置 SSL 或 TLS 功能。那,或者服务器以某种方式期望 Powershell 连接而不是 Postman 或 Python。请注意,我已将 # -SslProtocol Tls 注释掉,因为我使用的是 SslProtocol 不存在的 Powershell 5.1。

最终,答案非常简单:我下载了错误的公司证书。从 Chrome 下载 SSL 证书时,会出现一个“证书路径”选项卡。我的有 3 个证书,我称它们为 A、B 和 C。B 是 A 的后代,C 是 B 的后代。我错误地下载了最低级别的 C。当我下载 A 并将其放入我的 cacert.pem 文件时,它起作用了。