xiddiqui commited on
Commit
1bb0c9b
·
1 Parent(s): 73ab019

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -13,12 +13,16 @@ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
13
 
14
  # Instead of reading from file, get credentials from environment variables
15
  CLIENT_CONFIG = {
16
- "installed": {
17
  "client_id": os.environ.get('GOOGLE_CLIENT_ID'),
18
  "client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
19
- "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"],
 
 
 
20
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
21
  "token_uri": "https://oauth2.googleapis.com/token",
 
22
  }
23
  }
24
 
@@ -48,16 +52,24 @@ def get_gmail_service(state_dict):
48
  if creds and creds.expired and creds.refresh_token:
49
  creds.refresh(Request())
50
  else:
51
- # Create flow without running local server
52
- flow = InstalledAppFlow.from_client_config(CLIENT_CONFIG, SCOPES)
53
- # Generate authorization URL
 
 
 
 
 
54
  auth_url, _ = flow.authorization_url(
55
  access_type='offline',
56
- include_granted_scopes='true'
 
57
  )
58
 
59
- # Return the authorization URL to the user
60
- return f"Please visit this URL to authorize the application: {auth_url}\n\nAfter authorization, you will receive a code. Please enter it in the application."
 
 
61
 
62
  return build('gmail', 'v1', credentials=creds)
63
 
@@ -193,6 +205,9 @@ def create_interface():
193
  gr.Markdown("# 📧 Smart Email Filter")
194
  gr.Markdown("Connect to your Gmail account to filter important emails")
195
 
 
 
 
196
  with gr.Row():
197
  days_back = gr.Slider(
198
  minimum=1,
@@ -217,10 +232,11 @@ def create_interface():
217
  show_copy_button=True
218
  )
219
 
 
220
  fetch_button.click(
221
  fn=fetch_emails,
222
  inputs=[days_back, include_job, include_personal],
223
- outputs=output
224
  )
225
 
226
  return demo
 
13
 
14
  # Instead of reading from file, get credentials from environment variables
15
  CLIENT_CONFIG = {
16
+ "web": {
17
  "client_id": os.environ.get('GOOGLE_CLIENT_ID'),
18
  "client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
19
+ "redirect_uris": [
20
+ "https://your-app-name.hf.space/oauth2callback",
21
+ "https://your-app-name.hf.space/"
22
+ ],
23
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
24
  "token_uri": "https://oauth2.googleapis.com/token",
25
+ "javascript_origins": ["https://your-app-name.hf.space"]
26
  }
27
  }
28
 
 
52
  if creds and creds.expired and creds.refresh_token:
53
  creds.refresh(Request())
54
  else:
55
+ # Configure OAuth2 with redirect URI
56
+ flow = InstalledAppFlow.from_client_config(
57
+ CLIENT_CONFIG,
58
+ SCOPES,
59
+ redirect_uri="https://your-app-name.hf.space/oauth2callback" # Replace with your Space's URL
60
+ )
61
+
62
+ # Generate authorization URL with proper redirect
63
  auth_url, _ = flow.authorization_url(
64
  access_type='offline',
65
+ include_granted_scopes='true',
66
+ prompt='consent'
67
  )
68
 
69
+ # Create an iframe for Google Sign-In
70
+ return f"""
71
+ <iframe src="{auth_url}" width="100%" height="600px" frameborder="0"></iframe>
72
+ """
73
 
74
  return build('gmail', 'v1', credentials=creds)
75
 
 
205
  gr.Markdown("# 📧 Smart Email Filter")
206
  gr.Markdown("Connect to your Gmail account to filter important emails")
207
 
208
+ # Add HTML component for OAuth iframe
209
+ auth_html = gr.HTML()
210
+
211
  with gr.Row():
212
  days_back = gr.Slider(
213
  minimum=1,
 
232
  show_copy_button=True
233
  )
234
 
235
+ # Update the click handler
236
  fetch_button.click(
237
  fn=fetch_emails,
238
  inputs=[days_back, include_job, include_personal],
239
+ outputs=[auth_html, output]
240
  )
241
 
242
  return demo