Spaces:
Sleeping

fahmialfayadh commited on
Commit
4709682
·
1 Parent(s): ec88b63

fix(gapi): handle refresh exceptions gracefully and inject environment client credentials

Browse files
skills/productivity/google-workspace/scripts/google_api.py CHANGED
@@ -185,18 +185,30 @@ def get_credentials():
185
  from google.oauth2.credentials import Credentials
186
  from google.auth.transport.requests import Request
187
 
188
- creds = Credentials.from_authorized_user_file(str(TOKEN_PATH), _stored_token_scopes())
 
 
 
 
 
 
 
 
 
 
 
 
189
  if creds.expired and creds.refresh_token:
190
- creds.refresh(Request())
191
- TOKEN_PATH.write_text(
192
- json.dumps(
193
- _normalize_authorized_user_payload(json.loads(creds.to_json())),
194
- indent=2,
195
- ), encoding="utf-8"
196
- )
197
- if not creds.valid:
198
- print("Token is invalid. Re-run setup.", file=sys.stderr)
199
- sys.exit(1)
200
  return creds
201
 
202
 
 
185
  from google.oauth2.credentials import Credentials
186
  from google.auth.transport.requests import Request
187
 
188
+ try:
189
+ token_data = json.loads(TOKEN_PATH.read_text(encoding="utf-8"))
190
+ except Exception:
191
+ token_data = {}
192
+
193
+ env_cid = os.environ.get("GOOGLE_CLIENT_ID")
194
+ env_csec = os.environ.get("GOOGLE_CLIENT_SECRET")
195
+ if env_cid:
196
+ token_data["client_id"] = env_cid
197
+ if env_csec:
198
+ token_data["client_secret"] = env_csec
199
+
200
+ creds = Credentials.from_authorized_user_info(token_data, _stored_token_scopes()) if token_data else Credentials.from_authorized_user_file(str(TOKEN_PATH), _stored_token_scopes())
201
  if creds.expired and creds.refresh_token:
202
+ try:
203
+ creds.refresh(Request())
204
+ TOKEN_PATH.write_text(
205
+ json.dumps(
206
+ _normalize_authorized_user_payload(json.loads(creds.to_json())),
207
+ indent=2,
208
+ ), encoding="utf-8"
209
+ )
210
+ except Exception as e:
211
+ print(f"[Google Auth Refresh Warning] {e}", file=sys.stderr)
212
  return creds
213
 
214