_ssl.c:997 SSLError Smartsheet API

130 Views Asked by At

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.

1

There are 1 best solutions below

3
Kim Brandl On

With the latest version of the Smartsheet SDK installed (v3.0.2), I don't get any errors from running the code you posted:

import smartsheet

api_key = "MY_ACCESS_TOKEN_HERE"

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)

Running this code produces the following output (not super helpful output, but what I would expect the code to produce, and no error):

Hello World

[<smartsheet.models.sheet.Sheet object at 0x000001FDF0137D00>, <smartsheet.models.sheet.Sheet object at 0x000001FDF01729A0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF017B6A0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF017D370>, <smartsheet.models.sheet.Sheet object at 0x000001FDF017F040>, <smartsheet.models.sheet.Sheet object at 0x000001FDF017FCD0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF01819A0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0183670>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0184340>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0184FD0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0186CA0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0188970>, <smartsheet.models.sheet.Sheet object at 0x000001FDF018A640>, <smartsheet.models.sheet.Sheet object at 0x000001FDF018C310>, <smartsheet.models.sheet.Sheet object at 0x000001FDF018CFA0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF018FC70>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0990940>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0992610>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09942E0>, 
<smartsheet.models.sheet.Sheet object at 0x000001FDF0994F70>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0996C40>, <smartsheet.models.sheet.Sheet object at 0x000001FDF0998910>, 
<smartsheet.models.sheet.Sheet object at 0x000001FDF09995E0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF099B2B0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF099BF40>, <smartsheet.models.sheet.Sheet object at 0x000001FDF099DC10>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A08E0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A25B0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A3280>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A3F10>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A5BE0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A78B0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09A9580>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09AB250>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09ABEE0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09ADBB0>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09AE880>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09B0550>, <smartsheet.models.sheet.Sheet object at 0x000001FDF09B3220>]

First, I'd suggest that you verify you're using the latest version of the Smartsheet SDK.

  1. To see what version you have installed: pip show smartsheet-python-sdk

  2. To upgrade the SDK to its latest version (3.0.2): pip install smartsheet-python-sdk --upgrade

  3. Verify that you now have v3.0.2 installed: pip show smartsheet-python-sdk

Then 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!