首页 > 解决方案 > 我在 python 中使用 gspread 模块时遇到了麻烦

问题描述

我打算访问谷歌电子表格以使用 python gspread 编写数据。我使用了以下代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials


scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

sheet = client.open('Copy of Google Merchant Center feed - Di: Produktflöde').sheet1

list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

这是来自https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html的示例代码,我创建了所需的凭据和客户端机密 json 文件。

但是我遇到了麻烦,找不到任何解决方案。

错误如下

Traceback (most recent call last):
  File "Bot2.py", line 16, in <module>
    client = gspread.authorize(creds)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\gspread\__init__.py", line 38, in authorize
    client.login()
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\gspread\client.py", line 51, in login
    self.auth.refresh(http)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\oauth2client\client.py", line 545, in refresh
    self._refresh(http)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\oauth2client\client.py", line 749, in _refresh
    self._do_refresh_request(http)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\oauth2client\client.py", line 780, in _do_refresh_request
    body=body, headers=headers)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\oauth2client\transport.py", line 282, in request
    connection_type=connection_type)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\httplib2\__init__.py", line 1957, in request
    cachekey,
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\httplib2\__init__.py", line 1622, in _request
    conn, request_uri, method, body, headers
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\httplib2\__init__.py", line 1528, in _conn_request
    conn.connect()
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\httplib2\__init__.py", line 1376, in connect
    raise socket_err
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\site-packages\httplib2\__init__.py", line 1311, in connect
    self.sock = self._context.wrap_socket(sock, server_hostname=self.host)
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 423, in wrap_socket
    session=session
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 870, in _create
    self.do_handshake()
  File "C:\Users\Dragon\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

(有时,我看到的是这个错误TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond而不是 connectionreseterror。)

我试图从这个错误声明中解决这个问题,但我找到的所有答案都没有帮助。

ConnectionResetError: [WinError 10054] 现有连接被远程主机强行关闭

请给我任何解决方案。感谢您的考虑。

标签: pythonpython-3.xgoogle-sheets

解决方案


推荐阅读