MENG21 commited on
Commit
8394073
·
1 Parent(s): a1f2c90
Files changed (1) hide show
  1. app.py +7 -3
app.py CHANGED
@@ -108,6 +108,7 @@ def main():
108
  authorization_url, state = flow.authorization_url(prompt="consent")
109
  st.session_state['oauth_state'] = state
110
  st.session_state['oauth_state_time'] = time.time()
 
111
  st.markdown(f"[Login with Google]({authorization_url})")
112
  else:
113
  st.title('📚 Student Grade Lookup')
@@ -188,8 +189,9 @@ def main():
188
  # Function to handle OAuth callback
189
  def handle_callback():
190
  flow = create_flow()
191
- code = st.query_params.get("code")
192
- state = st.query_params.get("state")
 
193
 
194
  if code and state:
195
  if 'oauth_state' not in st.session_state or 'oauth_state_time' not in st.session_state:
@@ -215,6 +217,7 @@ def handle_callback():
215
  logging.debug("Token fetch successful")
216
  del st.session_state['oauth_state']
217
  del st.session_state['oauth_state_time']
 
218
  return credentials
219
  except InvalidGrantError as e:
220
  logging.error(f"InvalidGrantError: {str(e)}")
@@ -230,7 +233,8 @@ def handle_callback():
230
 
231
  if __name__ == '__main__':
232
  logging.debug("Starting the application")
233
- if 'code' in st.query_params and 'state' in st.query_params:
 
234
  logging.debug("Authorization code and state found in query parameters")
235
  handle_callback()
236
  st.rerun()
 
108
  authorization_url, state = flow.authorization_url(prompt="consent")
109
  st.session_state['oauth_state'] = state
110
  st.session_state['oauth_state_time'] = time.time()
111
+ st.experimental_set_query_params(state=state)
112
  st.markdown(f"[Login with Google]({authorization_url})")
113
  else:
114
  st.title('📚 Student Grade Lookup')
 
189
  # Function to handle OAuth callback
190
  def handle_callback():
191
  flow = create_flow()
192
+ query_params = st.experimental_get_query_params()
193
+ code = query_params.get("code", [None])[0]
194
+ state = query_params.get("state", [None])[0]
195
 
196
  if code and state:
197
  if 'oauth_state' not in st.session_state or 'oauth_state_time' not in st.session_state:
 
217
  logging.debug("Token fetch successful")
218
  del st.session_state['oauth_state']
219
  del st.session_state['oauth_state_time']
220
+ st.experimental_set_query_params() # Clear query params
221
  return credentials
222
  except InvalidGrantError as e:
223
  logging.error(f"InvalidGrantError: {str(e)}")
 
233
 
234
  if __name__ == '__main__':
235
  logging.debug("Starting the application")
236
+ query_params = st.experimental_get_query_params()
237
+ if 'code' in query_params and 'state' in query_params:
238
  logging.debug("Authorization code and state found in query parameters")
239
  handle_callback()
240
  st.rerun()