]> git.cworth.org Git - turbot/blobdiff - turbot/sheets.py
Add automatic refreshing of the Google Sheets token
[turbot] / 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))