Reputation: 1090
I have the following piece of code which attempts to capture a gspread.exceptions.APIError, usually as the result of a timeout of the Google spreadsheet credentials. _open_sheet requests a new set of credentials.
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
and from the following stack trace the gspread.exceptions.APIError is not caught:
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"
}
}
Any clues as to what has gone on with this code?
Upvotes: 3
Views: 2475
Reputation: 1
Same happens with me wile I'm writing the data in excel sheet after some period of time this error appears. Once I run my script again it works fine but fails after some time.
One thing that observed was while writing data if it reaches the end of the row in that sheet and there is no further row to write then it starts to throw such error.
So you need to catch this exception as below:
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.")
Upvotes: 0