burnash / gspread

Google Sheets Python API

Home Page:https://docs.gspread.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)` When opening sheet

hanleyhansen opened this issue · comments

After successfully authenticating I'm getting an SSL error whenever I try to open a sheet.

scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(GOOGLE_AUTH['client_email'], GOOGLE_AUTH['private_key'], scope)
gc = gspread.authorize(credentials)

sh = gc.open('Genesis-QuestionBank')
Traceback (most recent call last):
  File "genesis/bin/import_questions.py", line 27, in <module>
    sh = gc.open('Genesis-QuestionBank')
  File "/Users/administrator2/.virtualenvs/genesis/lib/python2.7/site-packages/gspread/client.py", line 134, in open
    feed = self.get_spreadsheets_feed()
  File "/Users/administrator2/.virtualenvs/genesis/lib/python2.7/site-packages/gspread/client.py", line 220, in get_spreadsheets_feed
    r = self.session.get(url)
  File "/Users/administrator2/.virtualenvs/genesis/lib/python2.7/site-packages/gspread/httpsession.py", line 79, in get
    return self.request('GET', url, **kwargs)
  File "/Users/administrator2/.virtualenvs/genesis/lib/python2.7/site-packages/gspread/httpsession.py", line 71, in request
    uri.scheme + uri.netloc].request(method, url, data, headers=request_headers)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1035, in _send_request
    self.endheaders(body)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 850, in _send_output
    self.send(msg)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 812, in send
    self.connect()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1212, in connect
    server_hostname=server_hostname)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 350, in wrap_socket
    _context=self)
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 566, in __init__
    self.do_handshake()
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 788, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

Are you using Python 2.7.9? As a quick-fix you can edit the client.py

import ssl
.
.
.
if hasattr(ssl, '_create_unverified_context'):
      ssl._create_default_https_context = ssl._create_unverified_context

It's not only a problem with Python 2.7.9. Both current versions 2.7.x and 3.4.x have implemented certificate verification as described in PEP476 and documented here.

A proper fix would be to use the systems default certificate store:
context = ssl.create_default_context()

The monkeypatch described above will fall back to the unverified behaviour.

@danielcb Could you elaborate on where to put this fix?

I just fixed it and am creating a pull request now :)

I'm getting the same error as OP. Is there a proper fix for this?

Try to update the time of your computer for instance with a NTP server

Any updates? I used "pip install gspread" today and am getting the OP's error. Date/time are accurate and sync'd with an NTP server.

Incorporating danielcb's fix resolved the issue for me.

Thanx! I missed the fact that danielcb's May 6 PR was still open. Worked like a charm!

Fixed in #255.

I'm seeing this issue again - it popped up this morning and I'm still trying to figure out the cause.

I'm seeing this issue too:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 559, in urlopen
    body=body, headers=headers)
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 784, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 252, in connect
    ssl_version=resolved_ssl_version)
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/util/ssl_.py", line 305, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/local/Cellar/python3/3.4.2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ssl.py", line 364, in wrap_socket
    _context=self)
  File "/usr/local/Cellar/python3/3.4.2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ssl.py", line 578, in __init__
    self.do_handshake()
  File "/usr/local/Cellar/python3/3.4.2/Frameworks/Python.framework/Versions/3.4/lib/python3.4/ssl.py", line 805, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/requests/adapters.py", line 376, in send
    timeout=timeout
  File "/usr/local/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 588, in urlopen
    raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "spree_ctl.py", line 181, in <module>
    down_sync()
  File "spree_ctl.py", line 69, in down_sync
    gsh = get_google_sheet()
  File "spree_ctl.py", line 54, in get_google_sheet
    gwb = gc.open('SpreeProducts')
  File "/usr/local/lib/python3.4/site-packages/gspread/client.py", line 145, in open
    feed = self.get_spreadsheets_feed()
  File "/usr/local/lib/python3.4/site-packages/gspread/client.py", line 231, in get_spreadsheets_feed
    r = self.session.get(url)
  File "/usr/local/lib/python3.4/site-packages/gspread/httpsession.py", line 75, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/gspread/httpsession.py", line 67, in request
    response = func(url, data=data, headers=request_headers)
  File "/usr/local/lib/python3.4/site-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.4/site-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/requests/adapters.py", line 447, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)

Is there any information on the cause or a potential temporary solution?

PS: Last run about 2 weeks ago, it was working fine then. This morning it's been giving this error.

I thought I posted a comment about what this was for me - it turned out the Python package "certifi" which is part of a other Python package "Tornado" was causing this error. I used:

pip uninstall -y certifi

And the error went away, and Tornado appeared to still work.

Update: Upgrading python and reinstalling dependencies caused the error to cease for me.

I'm also hitting this

python --version
Python 3.4.2

gspread.__version__
'0.3.0'

Testing with virtualenv in a Debian Jessie Vagrant box

Can confirm that sudo pip uninstall -y certifi solved the issue for me.

This issue is back, and now requests looks like it depends on certifi to function. I'll post here again if I figure it out.

So, I don't know what happened, but for some reasons python package Requests 2.16.0 started requiring Certifi, which causes this SSL error. I had to specify version 2.15.1 to be able to uninstall certifi and restore service to my automation.
pip install requests==2.15.1; pip uninstall -y certifi