Spaces:
Running
Running
Commit
·
4e13286
1
Parent(s):
1ee60d9
fixing relogin problem
Browse files- src/auth.py +21 -7
src/auth.py
CHANGED
@@ -136,15 +136,20 @@ def get_credentials():
|
|
136 |
credentials = Credentials.from_authorized_user_info(creds_data, SCOPES)
|
137 |
credentials_from_env = True # Mark as environment credentials
|
138 |
|
139 |
-
if
|
|
|
140 |
try:
|
|
|
141 |
credentials.refresh(GoogleAuthRequest())
|
|
|
142 |
except Exception as refresh_error:
|
143 |
-
|
|
|
144 |
|
145 |
return credentials
|
146 |
except Exception as e:
|
147 |
-
|
|
|
148 |
|
149 |
# Check for credentials file (CREDENTIAL_FILE now includes GOOGLE_APPLICATION_CREDENTIALS path if set)
|
150 |
if os.path.exists(CREDENTIAL_FILE):
|
@@ -152,7 +157,7 @@ def get_credentials():
|
|
152 |
with open(CREDENTIAL_FILE, "r") as f:
|
153 |
creds_data = json.load(f)
|
154 |
|
155 |
-
|
156 |
if "access_token" in creds_data and "token" not in creds_data:
|
157 |
creds_data["token"] = creds_data["access_token"]
|
158 |
|
@@ -163,16 +168,25 @@ def get_credentials():
|
|
163 |
# Mark as environment credentials if GOOGLE_APPLICATION_CREDENTIALS was used
|
164 |
credentials_from_env = bool(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
|
165 |
|
166 |
-
if
|
|
|
167 |
try:
|
|
|
168 |
credentials.refresh(GoogleAuthRequest())
|
|
|
169 |
save_credentials(credentials)
|
170 |
except Exception as refresh_error:
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
return credentials
|
174 |
except Exception as e:
|
175 |
-
|
|
|
176 |
|
177 |
client_config = {
|
178 |
"installed": {
|
|
|
136 |
credentials = Credentials.from_authorized_user_info(creds_data, SCOPES)
|
137 |
credentials_from_env = True # Mark as environment credentials
|
138 |
|
139 |
+
# Try to refresh if expired and refresh token exists
|
140 |
+
if credentials.expired and credentials.refresh_token:
|
141 |
try:
|
142 |
+
logging.info("Environment credentials expired, attempting refresh...")
|
143 |
credentials.refresh(GoogleAuthRequest())
|
144 |
+
logging.info("Environment credentials refreshed successfully")
|
145 |
except Exception as refresh_error:
|
146 |
+
logging.warning(f"Failed to refresh environment credentials: {refresh_error}")
|
147 |
+
logging.info("Using existing environment credentials despite refresh failure")
|
148 |
|
149 |
return credentials
|
150 |
except Exception as e:
|
151 |
+
logging.warning(f"Failed to load environment credentials: {e}")
|
152 |
+
# Fall through to file-based credentials
|
153 |
|
154 |
# Check for credentials file (CREDENTIAL_FILE now includes GOOGLE_APPLICATION_CREDENTIALS path if set)
|
155 |
if os.path.exists(CREDENTIAL_FILE):
|
|
|
157 |
with open(CREDENTIAL_FILE, "r") as f:
|
158 |
creds_data = json.load(f)
|
159 |
|
160 |
+
# Handle different credential formats
|
161 |
if "access_token" in creds_data and "token" not in creds_data:
|
162 |
creds_data["token"] = creds_data["access_token"]
|
163 |
|
|
|
168 |
# Mark as environment credentials if GOOGLE_APPLICATION_CREDENTIALS was used
|
169 |
credentials_from_env = bool(os.getenv("GOOGLE_APPLICATION_CREDENTIALS"))
|
170 |
|
171 |
+
# Try to refresh if expired and refresh token exists
|
172 |
+
if credentials.expired and credentials.refresh_token:
|
173 |
try:
|
174 |
+
logging.info("File-based credentials expired, attempting refresh...")
|
175 |
credentials.refresh(GoogleAuthRequest())
|
176 |
+
logging.info("File-based credentials refreshed successfully")
|
177 |
save_credentials(credentials)
|
178 |
except Exception as refresh_error:
|
179 |
+
logging.warning(f"Failed to refresh file-based credentials: {refresh_error}")
|
180 |
+
logging.info("Using existing file-based credentials despite refresh failure")
|
181 |
+
elif not credentials.expired:
|
182 |
+
logging.info("File-based credentials are still valid, no refresh needed")
|
183 |
+
elif not credentials.refresh_token:
|
184 |
+
logging.warning("File-based credentials expired but no refresh token available")
|
185 |
|
186 |
return credentials
|
187 |
except Exception as e:
|
188 |
+
logging.error(f"Failed to load credentials from file {CREDENTIAL_FILE}: {e}")
|
189 |
+
# Fall through to new login only if credentials are completely unusable
|
190 |
|
191 |
client_config = {
|
192 |
"installed": {
|