From 00189acdb2d8040ee1198e461628b5d4b4de467e Mon Sep 17 00:00:00 2001 From: Henk Reder Date: Wed, 14 Jun 2023 16:03:13 -0400 Subject: [PATCH 1/4] localized timezone for expiry set in token --- src/gphotos_sync/authorize.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/gphotos_sync/authorize.py b/src/gphotos_sync/authorize.py index 488ad2f6..60cbd3ae 100644 --- a/src/gphotos_sync/authorize.py +++ b/src/gphotos_sync/authorize.py @@ -2,6 +2,7 @@ from json import JSONDecodeError, dump, load from pathlib import Path from typing import List, Optional +import time from google_auth_oauthlib.flow import InstalledAppFlow from requests.adapters import HTTPAdapter @@ -75,6 +76,14 @@ def save_token(self, token: str): dump(token, stream) self.token_file.chmod(0o600) + def convert_to_local_timestamp(self, dt): + dt = dt.astimezone() + tz_offset = 86400 - dt.tzinfo.utcoffset(dt).seconds + # Daylight Savings Time + is_dst = time.localtime().tm_isdst + tz_offset = tz_offset + 3600 if is_dst else tz_offset + return dt.timestamp() - tz_offset + def authorize(self): """Initiates OAuth2 authentication and authorization flow""" token = self.load_token() @@ -104,7 +113,7 @@ def authorize(self): "refresh_token": flow.credentials.refresh_token, "token_type": "Bearer", "scope": flow.credentials.scopes, - "expires_at": flow.credentials.expiry.timestamp(), + "expires_at": self.convert_to_local_timestamp(flow.credentials.expiry), } self.save_token(oauth2_token) From c1670f73c0bed0edaa69d4e7d9ce7fdfc0c371cd Mon Sep 17 00:00:00 2001 From: Henk Reder Date: Wed, 14 Jun 2023 18:16:49 -0400 Subject: [PATCH 2/4] corrected function to offset timestamp based on system timezone. --- src/gphotos_sync/authorize.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/gphotos_sync/authorize.py b/src/gphotos_sync/authorize.py index 60cbd3ae..3150c883 100644 --- a/src/gphotos_sync/authorize.py +++ b/src/gphotos_sync/authorize.py @@ -77,12 +77,9 @@ def save_token(self, token: str): self.token_file.chmod(0o600) def convert_to_local_timestamp(self, dt): - dt = dt.astimezone() - tz_offset = 86400 - dt.tzinfo.utcoffset(dt).seconds - # Daylight Savings Time is_dst = time.localtime().tm_isdst - tz_offset = tz_offset + 3600 if is_dst else tz_offset - return dt.timestamp() - tz_offset + offset = time.timezone - 3600 if is_dst else time.timezone + return dt.timestamp() - offset def authorize(self): """Initiates OAuth2 authentication and authorization flow""" From f1a79a2dd3ded442f06b75f65370351373e5cdd6 Mon Sep 17 00:00:00 2001 From: Henk Reder Date: Sat, 24 Jun 2023 15:58:52 -0400 Subject: [PATCH 3/4] matched documentation to python docs Co-authored-by: Jan Dolecek --- src/gphotos_sync/authorize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gphotos_sync/authorize.py b/src/gphotos_sync/authorize.py index 3150c883..7fb94c5e 100644 --- a/src/gphotos_sync/authorize.py +++ b/src/gphotos_sync/authorize.py @@ -110,7 +110,7 @@ def authorize(self): "refresh_token": flow.credentials.refresh_token, "token_type": "Bearer", "scope": flow.credentials.scopes, - "expires_at": self.convert_to_local_timestamp(flow.credentials.expiry), + "expires_at": flow.credentials.expiry.replace(tzinfo=timezone.utc).timestamp() } self.save_token(oauth2_token) From d8479a1a62c0daa5c0ff9108c058cc12be477a9c Mon Sep 17 00:00:00 2001 From: Henk Reder Date: Sat, 24 Jun 2023 16:04:15 -0400 Subject: [PATCH 4/4] added timezone; cleaned up extranious code --- src/gphotos_sync/authorize.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/gphotos_sync/authorize.py b/src/gphotos_sync/authorize.py index 7fb94c5e..ea7b1441 100644 --- a/src/gphotos_sync/authorize.py +++ b/src/gphotos_sync/authorize.py @@ -2,7 +2,7 @@ from json import JSONDecodeError, dump, load from pathlib import Path from typing import List, Optional -import time +from datetime import timezone from google_auth_oauthlib.flow import InstalledAppFlow from requests.adapters import HTTPAdapter @@ -76,11 +76,6 @@ def save_token(self, token: str): dump(token, stream) self.token_file.chmod(0o600) - def convert_to_local_timestamp(self, dt): - is_dst = time.localtime().tm_isdst - offset = time.timezone - 3600 if is_dst else time.timezone - return dt.timestamp() - offset - def authorize(self): """Initiates OAuth2 authentication and authorization flow""" token = self.load_token()