We try to read the content of a Google Sheet from a Linux server behind a corporate proxy. Generally we are able to connect to the Internet through the proxy, e.g. pip install or python requests work fine. But the following does not:
export GOOGLE_APPLICATION_CREDENTIALS="path to our credentials file"
python
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SAMPLE_SPREADSHEET_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
SAMPLE_RANGE_NAME = 'Sheet1!A1:J17'
service = build('sheets', 'v4')
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME).execute()
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/googleapiclient/http.py", line 851, in execute
method=str(self.method), body=self.body, headers=self.headers)
File "/usr/local/lib/python3.6/site-packages/googleapiclient/http.py", line 184, in _retry_request
raise exception
File "/usr/local/lib/python3.6/site-packages/googleapiclient/http.py", line 165, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/google_auth_httplib2.py", line 187, in request
self._request, method, uri, request_headers)
File "/usr/local/lib/python3.6/site-packages/google/auth/credentials.py", line 122, in before_request
self.refresh(request)
File "/usr/local/lib/python3.6/site-packages/google/oauth2/service_account.py", line 322, in refresh
request, self._token_uri, assertion)
File "/usr/local/lib/python3.6/site-packages/google/oauth2/_client.py", line 145, in jwt_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/usr/local/lib/python3.6/site-packages/google/oauth2/_client.py", line 106, in _token_endpoint_request
method='POST', url=token_uri, headers=headers, body=body)
File "/usr/local/lib/python3.6/site-packages/google_auth_httplib2.py", line 116, in __call__
url, method=method, body=body, headers=headers, **kwargs)
File "/usr/local/lib/python3.6/site-packages/httplib2/__init__.py", line 1957, in request
cachekey,
File "/usr/local/lib/python3.6/site-packages/httplib2/__init__.py", line 1622, in _request
conn, request_uri, method, body, headers
File "/usr/local/lib/python3.6/site-packages/httplib2/__init__.py", line 1528, in _conn_request
conn.connect()
File "/usr/local/lib/python3.6/site-packages/httplib2/__init__.py", line 1311, in connect
self.sock = self._context.wrap_socket(sock, server_hostname=self.host)
File "/usr/local/lib/python3.6/ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "/usr/local/lib/python3.6/ssl.py", line 817, in __init__
self.do_handshake()
File "/usr/local/lib/python3.6/ssl.py", line 1077, in do_handshake
self._sslobj.do_handshake()
File "/usr/local/lib/python3.6/ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)
How can we solve this issue?
Things we already checked:
- Code and credentials work when we are not behind corporate proxy.
- System clock is ok.
- Behind the proxy, we can connect to relevant URLs (https://sheets.googleapis.com and https://oauth2.googleapis.com) with curl without errors.