我的这段代码在 AWS 上完美运行。但是,我需要迁移到 Contabo 服务器,现在出现此错误。我已经在 StackOverflow 上搜索过,但那里没有答案,而且我在这里找到的解决方案很旧,对我不起作用。
我正在使用 Streamlit,有时我需要查阅 Googlesheets 中的电子表格
重现
import streamlit as st
import gspread
gc = gspread.service_account(filename="google.json")
planilha = gc.open_by_url(
"my_url"
)
以前没有发生过这种情况,现在我遇到了这个错误
TransportError: HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1000)')))
Traceback:
File "/usr/local/lib/python3.12/dist-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
exec(code, module.__dict__)
File "/root/mcenter_streamlit/app/1_📤_BUENOSHOPS_-_FULFILLMENT.py", line 2106, in <module>
planilha = gc.open_by_url(
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/gspread/client.py", line 227, in open_by_url
return self.open_by_key(extract_id_from_url(url))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/gspread/client.py", line 206, in open_by_key
spreadsheet = Spreadsheet(self, {"id": key})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/gspread/spreadsheet.py", line 37, in __init__
metadata = self.fetch_sheet_metadata()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/gspread/spreadsheet.py", line 245, in fetch_sheet_metadata
r = self.client.request("get", url, params=params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/gspread/client.py", line 80, in request
response = getattr(self.session, method)(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/requests/sessions.py", line 602, in get
return self.request("GET", url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/auth/transport/requests.py", line 537, in request
self.credentials.before_request(auth_request, method, url, request_headers)
File "/usr/local/lib/python3.12/dist-packages/google/auth/credentials.py", line 230, in before_request
self._blocking_refresh(request)
File "/usr/local/lib/python3.12/dist-packages/google/auth/credentials.py", line 193, in _blocking_refresh
self.refresh(request)
File "/usr/local/lib/python3.12/dist-packages/google/oauth2/service_account.py", line 445, in refresh
access_token, expiry, _ = _client.jwt_grant(
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/oauth2/_client.py", line 308, in jwt_grant
response_data = _token_endpoint_request(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/oauth2/_client.py", line 268, in _token_endpoint_request
response_status_ok, response_data, retryable_error = _token_endpoint_request_no_throw(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/oauth2/_client.py", line 215, in _token_endpoint_request_no_throw
request_succeeded, response_data, retryable_error = _perform_request()
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/oauth2/_client.py", line 191, in _perform_request
response = request(
^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/google/auth/transport/requests.py", line 192, in __call__
raise new_exc from caught_exc
环境信息:
Ubuntu 20.04.6 LTS Python 3.12.2 gspread 5.12.4
有人可以帮忙吗?
我最终找到了解决方案。
我安装了 CA 证书:
sudo apt-get update && sudo apt-get install ca-certificatessudo apt-get update && sudo apt-get install ca-certificates
然后我设置 SSL 证书的环境变量
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt