Skip to content

Commit

Permalink
Save credentials if client_id/secret have changed
Browse files Browse the repository at this point in the history
  • Loading branch information
anxuae committed Apr 27, 2022
1 parent ba0df2d commit 93f7671
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pibooth_google_photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pibooth.utils import LOGGER


__version__ = "1.2.1"
__version__ = "1.2.2"

SECTION = 'GOOGLE'
CACHE_FILE = '.google_token.json'
Expand Down Expand Up @@ -112,22 +112,22 @@ def _auth(self):

def _save_credentials(self, credentials):
"""Save credentials in a file to use API without need to allow acces."""
with open(self.token_cache_file, 'w') as fp:
fp.write(credentials.to_json())
try:
with open(self.token_cache_file, 'w') as fp:
fp.write(credentials.to_json())
except OSError as err:
LOGGER.warning("Can not save Google Photos token in file '%s': %s",
self.token_cache_file, err)

def _get_authorized_session(self):
"""Create credentials file if required and open a new session."""
credentials = None
if not os.path.exists(self.token_cache_file) or \
os.path.getsize(self.token_cache_file) == 0:
credentials = self._auth()
LOGGER.debug("First use of plugin, store token in file '%s'",
self.token_cache_file)
try:
self._save_credentials(credentials)
except OSError as err:
LOGGER.warning("Can not save Google Photos token in file '%s': %s",
self.token_cache_file, err)
credentials = self._auth()
self._save_credentials(credentials)
else:
credentials = Credentials.from_authorized_user_file(self.token_cache_file, self.SCOPES)
with open(self.client_id_file) as fd:
Expand All @@ -137,6 +137,7 @@ def _get_authorized_session(self):
LOGGER.debug("Application key or secret has changed, store new token in file '%s'",
self.token_cache_file)
credentials = self._auth()
self._save_credentials(credentials)
elif credentials.expired:
credentials.refresh(Request())
self._save_credentials(credentials)
Expand Down

0 comments on commit 93f7671

Please sign in to comment.