Google表格身份验证每隔几个小时就会过期

问题描述 投票:0回答:1

我目前正在使用gspread在Web服务器上不断访问Google表格文档。

它托管在Flask Web服务器(apache WSGI)上,因此与Google的连接必须连续且持续进行身份验证。

我有一个问题,几个小时后,我需要重新生成图书对象(重新验证)或者我会得到这个问题(见下文)。

以下是我为解决这个问题所做的工作,但我仍然遇到同样的问题,并且想知道你是否有任何想法。

有5-6个不同的工作表,我正在创建一个具有不同方法的类来访问每个工作表(所有工作都继承自Access)。

book = authorise()
employee, shift, clock, client, payment = Employee(book), Shift(book), 
Clock(book), Client(book), Payment(book)

def refresh_google_access(self): #CALLED EVERY 40 MINUTES
   book = authorise()
   employee._refresh(book)
   shift._refresh(book)
   clock._refresh(book)
   client._refresh(book)
   payment._refresh(book)

def authorise():
   scope = ['https://spreadsheets.google.com/feeds']
   credentials = ServiceAccountCredentials.from_json_keyfile_name('clocker/access.json', scope)
   gc = gspread.authorize(credentials)
   book = gc.open_by_key(SHEET_KEY)

   return book

class Access:
   def _refresh(self, book):
       self.__init__(book)

class Employee(Access):

   def __init__(self, book):
       self.sheet = book.worksheet("Employee List")

   ...

class Clock(Access):

   def __init__(self, book):
       self.sheet = book.worksheet("Time Log")

   ...

class Shift(Access):

   def __init__(self, book):
       self.sheet = book.worksheet("Shift List")
   ...

class Client(Access):

   def __init__(self, book):
       self.sheet = book.worksheet("Client List")

   ...

class Payment(Access):

   def __init__(self, book):
       self.sheet = book.worksheet("Payments")

   ...

几个小时后(4-24小时之间),我会收到此错误:

Traceback (most recent call last):
   File "/home/ve/lib/python3.4/site-packages/flask/app.py", line 2292, in wsgi_app
     response = self.full_dispatch_request()
   File "/home/ve/lib/python3.4/site-packages/flask/app.py", line 1815, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/home/ve/lib/python3.4/site-packages/flask/app.py", line 1718, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/home/ve/lib/python3.4/site-packages/flask/_compat.py", line 35, in reraise
     raise value
   File "/home/ve/lib/python3.4/site-packages/flask/app.py", line 1813, in full_dispatch_request
     rv = self.dispatch_request()
   File "/home/ve/lib/python3.4/site-packages/flask/app.py", line 1799, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/var/www/clocker/clocker/routes.py", line 43, in voice
     clocked_in = clock.is_clocked_in(id)
   File "/var/www/clocker/clocker/sheet_access.py", line 115, in is_clocked_in
     latest_clock_in = self.get_latest_clock_in(id)
   File "/var/www/clocker/clocker/sheet_access.py", line 107, in get_latest_clock_in
     result = self.sheet.findall(str(id))
   File "/home/ve/lib/python3.4/site-packages/gspread/models.py", line 814, in findall
     return list(self._finder(filter, query))
   File "/home/ve/lib/python3.4/site-packages/gspread/models.py", line 779, in _finder
     data = self.spreadsheet.values_get(self.title)
   File "/home/ve/lib/python3.4/site-packages/gspread/models.py", line 110, in values_get
     r = self.client.request('get', url, params=params)
   File "/home/ve/lib/python3.4/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"
   }
 }

只需重新启动服务器,几乎每次都可以修复此问题。

重新生成书籍对象似乎无法解决问题,我已经检查过该书的id每40分钟更改一次。

flask google-apps-script google-sheets google-sheets-api gspread
1个回答
0
投票

如果我理解正确,在我的使用中,我会抓住gc并在需要数据时调用book = gc.open_by_key(SHEET_KEY)

基本上不是将book保存为全局变量,而是保存gc,以便保留先前经过身份验证的会话并用于进一步的请求。

© www.soinside.com 2019 - 2024. All rights reserved.