]> git.cworth.org Git - turbot/commitdiff
Add automatic refreshing of the Google Sheets token
authorCarl Worth <cworth@cworth.org>
Fri, 9 Oct 2020 21:09:23 +0000 (14:09 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 9 Oct 2020 21:12:11 +0000 (14:12 -0700)
Otherwise, the original token expires and becomes useless after 60
minutes.

turbot/sheets.py

index 7e6ddeaea20efccf690f6429c162579d6c307214..a578b100267243ff07539afc375637a95e177c98 100644 (file)
@@ -3,6 +3,7 @@ import pickle
 import os.path
 import os
 
+from google.auth.transport.requests import Request
 from googleapiclient.discovery import build
 
 TEMPLATE_SHEET_ID = "1drSoyrE4gM3JaGweDkOybwXWdKPIDTfUmB1gQCYS3Uw"
@@ -26,6 +27,10 @@ def sheets_create(name):
             with open(TOKEN_FILE, 'rb') as token:
                 creds = pickle.load(token)
 
+    # Refresh credentials if necessary
+    if creds and not creds.valid:
+        creds.refresh(Request())
+
     # If there are no (valid) credentials available, give up
     if not creds or not creds.valid:
         current_app.logger.error("No token found in {}".format(TOKEN_FILE))