我有以下代码尝试捕获gspread.exceptions.APIError,通常是由于Google电子表格凭据超时导致的。 _open_sheet请求一组新的凭据。
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/app/api_1_0/views.py", line 79, in lead
statSheet.updateStats(config.leadsheetTile)
File "/app/app/statSheet.py", line 71, in updateStats
row = self._findRow(entry)
File "/app/app/statSheet.py", line 54, in _findRow
cell = self.worksheet.find(entry)
File "/app/.heroku/python/lib/python3.6/site-packages/gspread/models.py", line 805, in find
return self._finder(finditem, query)
File "/app/.heroku/python/lib/python3.6/site-packages/gspread/models.py", line 779, in _finder
data = self.spreadsheet.values_get(self.title)
File "/app/.heroku/python/lib/python3.6/site-packages/gspread/models.py", line 110, in values_get
r = self.client.request('get', url, params=params)
File "/app/.heroku/python/lib/python3.6/site-packages/gspread/client.py", line 79, in request
raise APIError(response)
gspread.exceptions.APIError: {
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
,并从以下堆栈中跟踪未捕获到gspread.exceptions.APIError:
NUMBER
有关此代码发生了什么的任何线索?
答案 0 :(得分:0)
我遇到了同样的问题,起作用的是:
except gspread.CellNotFound:
答案 1 :(得分:0)
我会在发生此错误一段时间后将数据写入excel工作表中时发生同样的情况。 一旦我再次运行脚本,它就可以正常运行,但一段时间后会失败。
观察到的一件事是在写入数据时,如果到达该表中该行的末尾并且没有其他行要写入,那么它将开始引发此类错误。
因此,您需要捕获以下异常:
try:
Sheet.update_cell(row, column, value="What ever value")t_term)
except gspread.exceptions.APIError as UNAUTHENTICATED:
print("Api error occurred for gspread due to authentication failure while writing What ever value.")