大约六个月前,我写了一些代码来访问Smartsheet API,当我回到它时,它不再工作。例如,
import smartsheet
api_key = "ABCD1234"
smartsheet_client = smartsheet.Smartsheet(api_key)
smartsheet_client.errors_as_exceptions(True)
print("Hello World\n")
sheets = smartsheet_client.Sheets.list_sheets(include_all=True).data
print(sheets)
字符串
现在将输出:
Hello World
Traceback (most recent call last): File "C:\Users\...\test_smartsheet\virtenv\lib\site-packages\urllib3\connectionpool.py", line 468, in _make_request self._validate_conn(conn) ... File "C:\Users\...\AppData\Local\Programs\Python\Python310\lib\ssl.py", line 1341, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997)
During handling of the above exception, another exception occurred:
...
The above exception was the direct cause of the following exception:
During handling of the above exception, another exception occurred:
The above exception was the direct cause of the following exception:
...
File "C:\Users\...\test_smartsheet\virtenv\lib\site-packages\smartsheet\smartsheet.py", line 335, in _request raise HttpError(rex, "SSL handshake error, old CA bundle or old OpenSSL?") from rex smartsheet.exceptions.HttpError: (SSLError(MaxRetryError("HTTPSConnectionPool(host='api.smartsheet.com', port=443): Max retries exceeded with url: /2.0/sheets?includeAll=True (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get issuer certificate (_ssl.c:997)')))")), 'SSL handshake error, old CA bundle or old OpenSSL?')
型
我尝试更改请求和证书的版本,并使用“api-smartsheet-com-chain”(100-PKROOTCA 290-CA PEM(链)文件)(.\test_smartsheet\virtenv\Lib\site-packages\certifici\cacert.pem)更新cacert. pem文件。
当我尝试:
curl -X GET -H "Authorization: Bearer JKlMNOpQ12RStUVwxYZAbcde3F5g6hijklM789" "https://api.smartsheet.com/2.0/sheets"
型
从Smartsheet API documentation,我得到:
curl :(35)舍内尔:next InitializeSecurityContext失败:未知错误(0x 80092012)-吊销功能无法检查证书的吊销。
但是当我尝试在末尾添加“--ssl-no-revoke”时,它起作用了。
我也试
import requests
smartsheet_access_token = "ABCD1234"
headers = {f"Authorization": "Bearer {smartsheet_access_token}"}
good = lambda x: True if int(x)>500 else False
res = requests.get("https://api.smartsheet.com/2.0/sheets",headers=headers,verify='verify.pem')
print(good(res.headers['Content-Length']))
print(res.content)
型
其中verify.pem是api-smartsheet-com-chain文件。
错误b“{\n“errorCode”:1002,\n“message”:“您的访问令牌无效。",\n“refId”:“f4 xhbm”\n}“
即使我尝试重新生成一个新的访问令牌。
如何解决此SSL问题(最好使用Smartsheet Python sdk)?
- 更新 * 我在另一个设备上运行代码,它工作了。在重新创建我的虚拟环境后,代码工作了大约一天,在运行之间返回SSL握手错误。
1条答案
按热度按时间drnojrws1#
安装了最新版本的Smartsheet SDK(v3.0.2),我在运行您发布的代码时没有遇到任何错误:
字符串
运行这段代码会产生以下输出(不是超级有用的输出,而是我期望代码产生的输出,并且没有错误):
型
首先,我建议您验证您使用的是最新版本的Smartsheet SDK。
1.查看您安装的版本:
pip show smartsheet-python-sdk
pip install smartsheet-python-sdk --upgrade
1.验证您现在是否已安装v3.0.2:
pip show smartsheet-python-sdk
然后尝试再次运行您发布的第一段代码(即我在这里发布的相同代码),看看是否得到无错误的结果。似乎Smartsheet几个月前将其SDK仓库从一个GitHub org(smartsheet-platform)移动到另一个(smartsheet),并且SDK可能在您最初安装它之后发生了一些变化。
如果你在使用最新版本的SDK时仍然出现错误,那么我建议你检查一下你正在运行的Python版本。有时候像这样的错误是由于SDK代码和你正在使用的Python版本之间存在某种不兼容性。值得一提的是,我上面描述的成功输出是在Python v3.9.1中观察到的。
当然,不用说,您还应该验证您的API访问令牌是否有效--但听起来您可能已经验证了这一点。
希望这是有帮助的,请在这里添加评论,如果你能解决这个问题,因为它可能会帮助别人在未来.谢谢!