MENG21 commited on
Commit
98f6576
·
1 Parent(s): 0fe31c2
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -9,6 +9,8 @@ from google.oauth2.credentials import Credentials
9
  from google_auth_oauthlib.flow import Flow
10
  from google.auth.transport.requests import Request
11
  import json
 
 
12
 
13
  # Load environment variables from .env file
14
  load_dotenv()
@@ -58,10 +60,12 @@ SCOPES = ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googlea
58
 
59
  # Function to create OAuth flow
60
  def create_flow():
 
 
61
  flow = Flow.from_client_config(
62
  client_config=CLIENT_CONFIG,
63
  scopes=SCOPES,
64
- redirect_uri="https://meng21-gradeview.hf.space/" # Update this with your Streamlit app's URL
65
  )
66
  return flow
67
 
@@ -179,15 +183,32 @@ def handle_callback():
179
  flow = create_flow()
180
  code = st.query_params.get("code")
181
  if code:
182
- flow.fetch_token(code=code)
183
- credentials = flow.credentials
184
- st.session_state['credentials'] = credentials.to_json()
185
- return credentials
 
 
 
 
 
 
 
 
 
 
 
186
  else:
 
187
  st.error("No authorization code found in the URL parameters.")
188
 
 
 
 
189
  if __name__ == '__main__':
 
190
  if 'code' in st.query_params:
 
191
  handle_callback()
192
  st.rerun()
193
  main()
 
9
  from google_auth_oauthlib.flow import Flow
10
  from google.auth.transport.requests import Request
11
  import json
12
+ import logging
13
+ from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
14
 
15
  # Load environment variables from .env file
16
  load_dotenv()
 
60
 
61
  # Function to create OAuth flow
62
  def create_flow():
63
+ redirect_uri = "https://meng21-gradeview.hf.space/" # Update this with your Streamlit app's URL
64
+ logging.debug(f"Creating flow with redirect URI: {redirect_uri}")
65
  flow = Flow.from_client_config(
66
  client_config=CLIENT_CONFIG,
67
  scopes=SCOPES,
68
+ redirect_uri=redirect_uri
69
  )
70
  return flow
71
 
 
183
  flow = create_flow()
184
  code = st.query_params.get("code")
185
  if code:
186
+ try:
187
+ logging.debug(f"Attempting to fetch token with code: {code}")
188
+ flow.fetch_token(code=code)
189
+ credentials = flow.credentials
190
+ st.session_state['credentials'] = credentials.to_json()
191
+ logging.debug("Token fetch successful")
192
+ return credentials
193
+ except InvalidGrantError as e:
194
+ logging.error(f"InvalidGrantError: {str(e)}")
195
+ st.error("The authorization code has expired or is invalid. Please try logging in again.")
196
+ if 'credentials' in st.session_state:
197
+ del st.session_state['credentials']
198
+ except Exception as e:
199
+ logging.error(f"Unexpected error during token fetch: {str(e)}")
200
+ st.error(f"An unexpected error occurred: {str(e)}")
201
  else:
202
+ logging.warning("No authorization code found in the URL parameters.")
203
  st.error("No authorization code found in the URL parameters.")
204
 
205
+ # Set up logging
206
+ logging.basicConfig(level=logging.DEBUG)
207
+
208
  if __name__ == '__main__':
209
+ logging.debug("Starting the application")
210
  if 'code' in st.query_params:
211
+ logging.debug("Authorization code found in query parameters")
212
  handle_callback()
213
  st.rerun()
214
  main()