diff --git a/src/main/python/main.py b/src/main/python/main.py index 6c7d335..b11bc7c 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -6,6 +6,7 @@ from google.auth.transport.requests import Request from spreadsheet_handler import SpreadsheetHandler from widget_gallery import WidgetGallery +import google.auth.exceptions as GoogleExceptions from fbs_runtime.application_context.PyQt5 import ApplicationContext from PyQt5.QtWidgets import QApplication, QMessageBox from PyQt5.QtGui import QIcon, QFont @@ -59,9 +60,27 @@ # If there are no (valid) credentials available, let the user log in. if not CREDS or not CREDS.valid: if CREDS and CREDS.expired and CREDS.refresh_token: - CREDS.refresh(Request()) + try: + CREDS.refresh(Request()) + except GoogleExceptions.RefreshError: + error_message = "Your access token has expired. Please, complete the authentication flow once again, " + \ + "accessing your google account on the website" + \ + " that will be opened when you close this window." + print(error_message) + app = QApplication([]) + error_window = QMessageBox() + error_window.setWindowTitle("Expenses Tracker") + error_window.setWindowIcon(QIcon(ApplicationContext().get_resource("submit.ico"))) + error_window.setIcon(QMessageBox.Critical) + error_window.setText(error_message) + error_window.setStandardButtons(QMessageBox.Ok) + error_window.setDefaultButton(QMessageBox.Ok) + error_window.exec_() + + flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) + CREDS = flow.run_local_server(port=0) else: - error_message = "Please complete the authentication flow " + \ + error_message = "Please, complete the authentication flow " + \ "accessing your google account on the website" + \ " that will be opened when you close this window." print(error_message)