簡體   English   中英

未捕獲 python3 gspread 異常

[英]python3 gspread exception not caught

我有以下代碼嘗試捕獲 gspread.exceptions.APIError,通常是由於 Google 電子表格憑據超時造成的。 _open_sheet 請求一組新的憑據。

def _findRow(self, entry):
''' Return row index for row which contains "entry". '''
for i in range(10):
    try:
        cell = self.worksheet.find(email)
        return cell.row
    except gspread.exceptions.CellNotFound:
        return None
    except gspread.exceptions.APIError:
        # This occurs when we get a timeout of the authentication token
        # Need to reauthenticate
        self._open_sheet()
        continue
print('Leadsheet:find:: Failed to renew authorization after 10 attempts.')
return None

並從以下堆棧跟蹤 gspread.exceptions.APIError 沒有被捕獲:

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" 
  } 
} 

關於此代碼發生了什么的任何線索?

我有同樣的問題,有效的是:

except gspread.CellNotFound:

在出現此錯誤一段時間后,我正在將數據寫入 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.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM