awacke1 commited on
Commit
c97ecbe
Β·
verified Β·
1 Parent(s): e03697a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -3
app.py CHANGED
@@ -50,7 +50,40 @@ else:
50
  # Create the MSAL instance for acquiring tokens
51
  client_instance = get_msal_app()
52
 
53
- def get_access_token(code):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  client_instance = get_msal_app()
55
 
56
  try:
@@ -128,11 +161,41 @@ def clear_query_params():
128
  base_url = st.get_page_config().base_url_path
129
  st.write(f'<meta http-equiv="refresh" content="0; url={base_url}">', unsafe_allow_html=True)
130
  st.stop()
131
-
132
  def process_query_params():
133
  query_params = st.query_params
134
  st.write("Debug: All query parameters:", query_params)
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  if 'error' in query_params:
137
  error = query_params.get('error')
138
  error_description = query_params.get('error_description', 'No description provided')
@@ -232,7 +295,13 @@ def process_query_params2():
232
  # Display content or image based on the query
233
  display_content_or_image(query)
234
 
235
-
 
 
 
 
 
 
236
 
237
 
238
  # πŸƒβ€β™‚οΈ Main application function
@@ -241,6 +310,31 @@ def main():
241
 
242
  process_query_params()
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  if 'access_token' not in st.session_state:
245
  query_params = st.query_params
246
  if 'code' in query_params:
 
50
  # Create the MSAL instance for acquiring tokens
51
  client_instance = get_msal_app()
52
 
53
+
54
+ def get_access_token(code=None):
55
+ client_instance = get_msal_app()
56
+
57
+ try:
58
+ if code:
59
+ # Initial token acquisition
60
+ result = client_instance.acquire_token_by_authorization_code(
61
+ code=code,
62
+ scopes=SCOPES,
63
+ redirect_uri=REDIRECT_URI
64
+ )
65
+ elif 'refresh_token' in st.session_state:
66
+ # Token refresh
67
+ result = client_instance.acquire_token_by_refresh_token(
68
+ refresh_token=st.session_state['refresh_token'],
69
+ scopes=SCOPES
70
+ )
71
+ else:
72
+ raise Exception("No authorization code or refresh token available")
73
+
74
+ if 'access_token' in result:
75
+ st.session_state['access_token'] = result['access_token']
76
+ if 'refresh_token' in result:
77
+ st.session_state['refresh_token'] = result['refresh_token']
78
+ return result['access_token']
79
+ else:
80
+ error_description = result.get('error_description', 'No error description provided')
81
+ raise Exception(f"Error acquiring token: {error_description}")
82
+ except Exception as e:
83
+ st.error(f"Exception in get_access_token: {str(e)}")
84
+ raise
85
+
86
+ def get_access_token4(code):
87
  client_instance = get_msal_app()
88
 
89
  try:
 
161
  base_url = st.get_page_config().base_url_path
162
  st.write(f'<meta http-equiv="refresh" content="0; url={base_url}">', unsafe_allow_html=True)
163
  st.stop()
164
+
165
  def process_query_params():
166
  query_params = st.query_params
167
  st.write("Debug: All query parameters:", query_params)
168
 
169
+ if 'error' in query_params:
170
+ error = query_params.get('error')
171
+ error_description = query_params.get('error_description', 'No description provided')
172
+ st.error(f"Authentication Error: {error}")
173
+ st.error(f"Error Description: {urllib.parse.unquote(error_description)}")
174
+ st.session_state.clear()
175
+ st.rerun()
176
+
177
+ if 'code' in query_params:
178
+ code = query_params.get('code')
179
+ st.write('πŸ”‘ Authorization Code Obtained:', code[:10] + '...')
180
+
181
+ try:
182
+ access_token = get_access_token(code)
183
+ st.session_state['access_token'] = access_token
184
+ st.success("Access token acquired successfully!")
185
+ st.rerun()
186
+ except Exception as e:
187
+ if "AADSTS70000" in str(e) and "code has expired" in str(e):
188
+ st.error("The authorization code has expired. Please log in again.")
189
+ st.session_state.clear()
190
+ st.rerun()
191
+ else:
192
+ st.error(f"Error acquiring access token: {str(e)}")
193
+ st.stop()
194
+
195
+ def process_query_params3():
196
+ query_params = st.query_params
197
+ st.write("Debug: All query parameters:", query_params)
198
+
199
  if 'error' in query_params:
200
  error = query_params.get('error')
201
  error_description = query_params.get('error_description', 'No description provided')
 
295
  # Display content or image based on the query
296
  display_content_or_image(query)
297
 
298
+ def get_user_info(access_token):
299
+ headers = {'Authorization': 'Bearer ' + access_token}
300
+ response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
301
+ if response.status_code == 200:
302
+ return response.json()
303
+ else:
304
+ raise Exception(f"Failed to fetch user info: {response.status_code} - {response.text}")
305
 
306
 
307
  # πŸƒβ€β™‚οΈ Main application function
 
310
 
311
  process_query_params()
312
 
313
+ #added
314
+ if 'access_token' not in st.session_state:
315
+ client_instance = get_msal_app()
316
+ authorization_url = client_instance.get_authorization_request_url(
317
+ scopes=SCOPES,
318
+ redirect_uri=REDIRECT_URI
319
+ )
320
+ st.write('πŸ‘‹ Please [click here]({}) to log in and authorize the app.'.format(authorization_url))
321
+ st.stop()
322
+
323
+ try:
324
+ # Attempt to use the access token
325
+ user_info = get_user_info(st.session_state['access_token'])
326
+ except Exception as e:
327
+ if "token has expired" in str(e).lower():
328
+ # Token expired, attempt to refresh
329
+ try:
330
+ new_token = get_access_token() # This will use the refresh token if available
331
+ st.session_state['access_token'] = new_token
332
+ user_info = get_user_info(new_token)
333
+ except Exception as refresh_error:
334
+ st.error("Failed to refresh token. Please log in again.")
335
+ st.session_state.clear()
336
+ st.rerun()
337
+
338
  if 'access_token' not in st.session_state:
339
  query_params = st.query_params
340
  if 'code' in query_params: