alxd commited on
Commit
ce7bfbc
·
1 Parent(s): 017affe

gmail auth with URI callback

Browse files
Files changed (1) hide show
  1. scoutLLM.py +42 -24
scoutLLM.py CHANGED
@@ -342,17 +342,22 @@ from google.auth.transport.requests import Request
342
  import os
343
  import json
344
 
 
345
  def get_gmail_credentials():
 
 
346
  creds = None
347
- token_path = "token.json"
348
 
349
  # Fetch client secrets from environment variables
350
- client_id = os.environ.get("HF_GMAIL_EMAIL") # Your client ID
351
- client_secret = os.environ.get("HF_GMAIL_PASSWORD") # Your client secret
352
 
353
  if not client_id or not client_secret:
354
  raise ValueError("Missing Gmail OAuth credentials in environment variables.")
355
 
 
 
 
356
  # Load credentials from token.json if available
357
  if os.path.exists(token_path):
358
  creds = Credentials.from_authorized_user_file(token_path)
@@ -360,40 +365,30 @@ def get_gmail_credentials():
360
  # If no valid credentials, log in via OAuth
361
  if not creds or not creds.valid:
362
  if creds and creds.expired and creds.refresh_token:
363
- creds.refresh(Request()) # Refresh token if expired
364
  else:
365
  client_config = {
366
- "installed": {
367
  "client_id": client_id,
368
- "project_id": "pendulum-8172e",
369
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
370
  "token_uri": "https://oauth2.googleapis.com/token",
371
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
372
  "client_secret": client_secret,
373
- "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"]
374
  }
375
  }
376
- flow = InstalledAppFlow.from_client_config(client_config, SCOPES)
377
-
378
- # Manually construct the URL with redirect_uri
379
- auth_url, _ = flow.authorization_url(
380
  prompt='consent',
381
  access_type='offline',
382
  include_granted_scopes='true'
383
  )
384
- auth_url += "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" # Explicitly add redirect_uri
385
 
386
- print("Go to the following URL in your browser:\n", auth_url)
387
- code = input("Enter the authorization code: ").strip()
388
- flow.fetch_token(code=code)
389
- creds = flow.credentials
390
-
391
- # Save the credentials for later use
392
- with open(token_path, "w") as token_file:
393
- token_file.write(creds.to_json())
394
-
395
- return creds
396
-
397
 
398
  # Add email sending function
399
  def send_email(email_address, content, is_formatted=True):
@@ -613,4 +608,27 @@ with gr.Blocks() as app:
613
 
614
  if __name__ == "__main__":
615
  debug_print("Launching Gradio UI...")
616
- app.queue().launch(share=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  import os
343
  import json
344
 
345
+
346
  def get_gmail_credentials():
347
+ global oauth_flow
348
+
349
  creds = None
 
350
 
351
  # Fetch client secrets from environment variables
352
+ client_id = os.environ.get("HF_GOOGLE_CLIENT_ID")
353
+ client_secret = os.environ.get("HF_GOOGLE_CLIENT_SECRET")
354
 
355
  if not client_id or not client_secret:
356
  raise ValueError("Missing Gmail OAuth credentials in environment variables.")
357
 
358
+ # Define the redirect URI for your Hugging Face space
359
+ redirect_uri = "https://huggingface.co/spaces/alx-d/scout/oauth2callback"
360
+
361
  # Load credentials from token.json if available
362
  if os.path.exists(token_path):
363
  creds = Credentials.from_authorized_user_file(token_path)
 
365
  # If no valid credentials, log in via OAuth
366
  if not creds or not creds.valid:
367
  if creds and creds.expired and creds.refresh_token:
368
+ creds.refresh(Request())
369
  else:
370
  client_config = {
371
+ "web": {
372
  "client_id": client_id,
373
+ "project_id": "your_project_id",
374
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
375
  "token_uri": "https://oauth2.googleapis.com/token",
376
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
377
  "client_secret": client_secret,
378
+ "redirect_uris": [redirect_uri]
379
  }
380
  }
381
+
382
+ oauth_flow = Flow.from_client_config(client_config, SCOPES, redirect_uri=redirect_uri)
383
+ auth_url, _ = oauth_flow.authorization_url(
 
384
  prompt='consent',
385
  access_type='offline',
386
  include_granted_scopes='true'
387
  )
 
388
 
389
+ return None, auth_url
390
+
391
+ return creds, None
 
 
 
 
 
 
 
 
392
 
393
  # Add email sending function
394
  def send_email(email_address, content, is_formatted=True):
 
608
 
609
  if __name__ == "__main__":
610
  debug_print("Launching Gradio UI...")
611
+ app.queue().launch(share=False)
612
+
613
+
614
+ @app.route("/oauth2callback")
615
+ def oauth2callback(request):
616
+ global oauth_flow
617
+
618
+ # Get the authorization code from the request
619
+ code = request.query_params.get("code")
620
+ if not code:
621
+ return "Authorization code missing"
622
+
623
+ try:
624
+ # Exchange the authorization code for credentials
625
+ oauth_flow.fetch_token(code=code)
626
+ creds = oauth_flow.credentials
627
+
628
+ # Save the credentials
629
+ with open(token_path, "w") as token_file:
630
+ token_file.write(creds.to_json())
631
+
632
+ return "Authentication successful! You can close this window and return to the application."
633
+ except Exception as e:
634
+ return f"Error during authentication: {str(e)}"