Spaces:
Sleeping
Sleeping
updated app.py
Browse files
app.py
CHANGED
@@ -14,10 +14,10 @@ SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
|
|
14 |
|
15 |
# Instead of reading from file, get credentials from environment variables
|
16 |
CLIENT_CONFIG = {
|
17 |
-
"
|
18 |
"client_id": os.environ.get('GOOGLE_CLIENT_ID'),
|
19 |
"client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
|
20 |
-
"redirect_uris": ["
|
21 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
22 |
"token_uri": "https://oauth2.googleapis.com/token",
|
23 |
}
|
@@ -124,9 +124,39 @@ def classify_email(email_data):
|
|
124 |
|
125 |
def fetch_emails(days_back, auth_code="", include_job=True, include_personal=True, progress=gr.Progress()):
|
126 |
"""Main function to fetch and filter emails"""
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
service = get_gmail_service(state_dict)
|
131 |
if isinstance(service, str):
|
132 |
# This means we got an auth URL instead of a service
|
|
|
14 |
|
15 |
# Instead of reading from file, get credentials from environment variables
|
16 |
CLIENT_CONFIG = {
|
17 |
+
"web": { # Changed to "web" for web credentials
|
18 |
"client_id": os.environ.get('GOOGLE_CLIENT_ID'),
|
19 |
"client_secret": os.environ.get('GOOGLE_CLIENT_SECRET'),
|
20 |
+
"redirect_uris": ["https://accounts.google.com/o/oauth2/auth"], # Standard Google OAuth endpoint
|
21 |
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
22 |
"token_uri": "https://oauth2.googleapis.com/token",
|
23 |
}
|
|
|
124 |
|
125 |
def fetch_emails(days_back, auth_code="", include_job=True, include_personal=True, progress=gr.Progress()):
|
126 |
"""Main function to fetch and filter emails"""
|
127 |
+
if not auth_code:
|
128 |
+
try:
|
129 |
+
# Configure OAuth2 flow for web application
|
130 |
+
flow = InstalledAppFlow.from_client_config(
|
131 |
+
CLIENT_CONFIG,
|
132 |
+
SCOPES,
|
133 |
+
redirect_uri="https://accounts.google.com/o/oauth2/auth" # Standard Google OAuth endpoint
|
134 |
+
)
|
135 |
+
|
136 |
+
# Generate authorization URL
|
137 |
+
auth_url, _ = flow.authorization_url(
|
138 |
+
access_type='offline',
|
139 |
+
include_granted_scopes='true'
|
140 |
+
)
|
141 |
+
|
142 |
+
return f"""Please follow these steps:
|
143 |
+
|
144 |
+
1. Click this link to authorize the application:
|
145 |
+
{auth_url}
|
146 |
+
|
147 |
+
2. Sign in with your Google account
|
148 |
+
3. Click 'Allow' to grant access
|
149 |
+
4. Copy the authorization code shown
|
150 |
+
5. Paste the code here and click 'Connect and Fetch Emails' again"""
|
151 |
|
152 |
try:
|
153 |
+
# Process the auth code and fetch emails
|
154 |
+
flow = InstalledAppFlow.from_client_config(
|
155 |
+
CLIENT_CONFIG,
|
156 |
+
SCOPES,
|
157 |
+
redirect_uri="https://accounts.google.com/o/oauth2/auth" # Same redirect URI here
|
158 |
+
)
|
159 |
+
|
160 |
service = get_gmail_service(state_dict)
|
161 |
if isinstance(service, str):
|
162 |
# This means we got an auth URL instead of a service
|