About six months ago, I had written some code to access the Smartsheet API which no longer works when I came back to it. For example,
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)
will now output:
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?')
I have tried changing my versions of requests and certifi and updating the cacert.pem file with "api-smartsheet-com-chain", the 100-PKROOTCA290-CA PEM(chain) file, (...\test_smartsheet\virtenv\Lib\site-packages\certifi\cacert.pem).
When I try:
curl -X GET -H "Authorization: Bearer JKlMNOpQ12RStUVwxYZAbcde3F5g6hijklM789" "https://api.smartsheet.com/2.0/sheets"
from the Smartsheet API documentation, I get:
curl: (35) schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - The revocation function was unable to check revocation for the certificate.
but when I try adding "--ssl-no-revoke" at the end it works.
I have also tried
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)
Where verify.pem is the api-smartsheet-com-chain file. This worked yesterday and now outputs
False b'{\n "errorCode" : 1002,\n "message" : "Your Access Token is invalid.",\n "refId" : "f4xhbm"\n}'
even after I tried regenerating a new access token.
How can I get this SSL issue resolved (preferably with the Smartsheet Python sdk)?
UPDATE I ran the code on another device and it worked. After recreating my virtual environment the code worked for about a day and the SSL handshake error returned between runs. This is still unsolved.
With the latest version of the Smartsheet SDK installed (v3.0.2), I don't get any errors from running the code you posted:
Running this code produces the following output (not super helpful output, but what I would expect the code to produce, and no error):
First, I'd suggest that you verify you're using the latest version of the Smartsheet SDK.
To see what version you have installed:
pip show smartsheet-python-sdkTo upgrade the SDK to its latest version (3.0.2):
pip install smartsheet-python-sdk --upgradeVerify that you now have v3.0.2 installed:
pip show smartsheet-python-sdkThen try running the first bit of code you posted again (i.e., the same code I posted here in my answer) to see if you get a non-error result. Seems like Smartsheet moved their SDK repos from one GitHub org (smartsheet-platform) to another (smartsheet) several months ago, and the SDK may have changed some since you originally installed it.
If you still get an error when using the latest version of the SDK, then I'd suggest you check what version of Python you're running. Sometimes errors like this happen due to some sort of incompatibility between the SDK code and the version of Python you're using. For what it's worth, the successful output I've described above was observed with Python v3.9.1.
Of course, it goes without saying that you should also verify that your API Access Token is valid -- but it sounds like you may have already verified such.
Hope this is helpful, and please add a comment here if you're able to resolve the problem, as it may help others in the future. Thanks!