Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,551 +2,208 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import msal
|
| 5 |
-
import urllib.parse
|
| 6 |
-
import base64
|
| 7 |
-
import hashlib
|
| 8 |
import secrets
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# New function to generate PKCE code verifier and challenge
|
| 23 |
-
def generate_pkce_codes():
|
| 24 |
-
code_verifier = secrets.token_urlsafe(128)[:128]
|
| 25 |
-
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode().rstrip('=')
|
| 26 |
-
return code_verifier, code_challenge
|
| 27 |
-
|
| 28 |
-
def get_msal_app(code_challenge=None):
|
| 29 |
-
return msal.PublicClientApplication(
|
| 30 |
-
client_id=APPLICATION_ID_KEY,
|
| 31 |
-
authority=AUTHORITY_URL
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# π οΈ Initialize the MSAL client
|
| 35 |
-
def get_msal_app_old():
|
| 36 |
return msal.ConfidentialClientApplication(
|
| 37 |
-
client_id=
|
| 38 |
-
client_credential=
|
| 39 |
-
authority=
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
raise
|
| 67 |
-
#st.stop()
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def get_access_token(code, code_verifier):
|
| 71 |
-
client_instance = get_msal_app()
|
| 72 |
-
|
| 73 |
-
st.write("Debug: MSAL App Configuration:")
|
| 74 |
-
st.write(f"Client ID: {APPLICATION_ID_KEY[:5]}...")
|
| 75 |
-
st.write(f"Authority: {AUTHORITY_URL}")
|
| 76 |
-
st.write(f"Redirect URI: {REDIRECT_URI}")
|
| 77 |
-
|
| 78 |
-
try:
|
| 79 |
-
result = client_instance.acquire_token_by_authorization_code(
|
| 80 |
-
code=code,
|
| 81 |
-
scopes=SCOPES,
|
| 82 |
-
redirect_uri=REDIRECT_URI,
|
| 83 |
-
code_verifier=code_verifier # Include the code verifier here
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
if 'access_token' in result:
|
| 87 |
-
return result['access_token']
|
| 88 |
-
else:
|
| 89 |
-
error_description = result.get('error_description', 'No error description provided')
|
| 90 |
-
raise Exception(f"Error acquiring token: {error_description}")
|
| 91 |
-
except Exception as e:
|
| 92 |
-
st.error(f"Exception in get_access_token: {str(e)}")
|
| 93 |
-
raise
|
| 94 |
-
|
| 95 |
-
def process_query_params():
|
| 96 |
-
# βοΈq= Run ArXiv search from query parameters
|
| 97 |
-
try:
|
| 98 |
-
query_params = st.query_params
|
| 99 |
-
st.write("Debug: All query parameters:", query_params)
|
| 100 |
-
|
| 101 |
-
if 'error' in query_params:
|
| 102 |
-
error = query_params.get('error')
|
| 103 |
-
error_description = query_params.get('error_description', 'No description provided')
|
| 104 |
-
st.error(f"Authentication Error: {error}")
|
| 105 |
-
st.error(f"Error Description: {urllib.parse.unquote(error_description)}")
|
| 106 |
-
st.stop()
|
| 107 |
-
|
| 108 |
-
if 'code' in query_params:
|
| 109 |
-
code = query_params.get('code')
|
| 110 |
-
st.write('π Authorization Code Obtained:', code[:10] + '...')
|
| 111 |
-
|
| 112 |
-
try:
|
| 113 |
-
code_verifier = st.session_state.get('code_verifier')
|
| 114 |
-
if not code_verifier:
|
| 115 |
-
st.error("Code verifier not found in session state.")
|
| 116 |
-
st.stop()
|
| 117 |
-
|
| 118 |
-
access_token = get_access_token(code, code_verifier)
|
| 119 |
-
st.session_state['access_token'] = access_token
|
| 120 |
-
st.success("Access token acquired successfully!")
|
| 121 |
-
# Clear the code from the URL
|
| 122 |
-
st.experimental_set_query_params()
|
| 123 |
-
st.rerun()
|
| 124 |
-
except Exception as e:
|
| 125 |
-
st.error(f"Error acquiring access token: {str(e)}")
|
| 126 |
-
st.stop()
|
| 127 |
-
else:
|
| 128 |
-
st.warning("No authorization code found in the query parameters.")
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
#result2 = search_glossary(result)
|
| 135 |
-
|
| 136 |
-
filesearch = PromptPrefix + query
|
| 137 |
-
st.markdown(filesearch)
|
| 138 |
-
process_text(filesearch)
|
| 139 |
-
except:
|
| 140 |
-
st.markdown(' ')
|
| 141 |
|
| 142 |
-
if '
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
#st.rerun()
|
| 149 |
|
| 150 |
-
if
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def main():
|
| 160 |
-
st.title("π¦ MS Graph API with AI & Cloud Integration
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
if
|
| 167 |
-
|
| 168 |
-
# Generate PKCE codes
|
| 169 |
-
code_verifier, code_challenge = generate_pkce_codes()
|
| 170 |
-
st.session_state['code_verifier'] = code_verifier
|
| 171 |
-
else:
|
| 172 |
-
code_verifier = st.session_state['code_verifier']
|
| 173 |
-
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode()).digest()).decode().rstrip('=')
|
| 174 |
-
|
| 175 |
-
client_instance = get_msal_app()
|
| 176 |
-
auth_url = client_instance.get_authorization_request_url(
|
| 177 |
-
scopes=SCOPES,
|
| 178 |
-
redirect_uri=REDIRECT_URI,
|
| 179 |
-
code_challenge=code_challenge,
|
| 180 |
-
code_challenge_method="S256"
|
| 181 |
-
)
|
| 182 |
-
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 183 |
-
#st.stop()
|
| 184 |
|
| 185 |
-
|
| 186 |
-
# π Sidebar navigation
|
| 187 |
st.sidebar.title("Navigation")
|
| 188 |
-
|
| 189 |
-
"
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
"
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
"
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
# ποΈ Product Integration Toggles
|
| 204 |
-
products = {
|
| 205 |
-
"π§ Outlook": {
|
| 206 |
-
"ai_capabilities": "Copilot for enhanced email writing, calendar management, and scheduling.",
|
| 207 |
-
"graph_api": "Access to mail, calendar, contacts, and events."
|
| 208 |
-
},
|
| 209 |
-
"π OneNote": {
|
| 210 |
-
"ai_capabilities": "Content suggestion, note organization, and OCR for extracting text from images.",
|
| 211 |
-
"graph_api": "Manage notebooks, sections, and pages."
|
| 212 |
-
},
|
| 213 |
-
"π Excel": {
|
| 214 |
-
"ai_capabilities": "Copilot for advanced data analysis, data insights, and formula generation.",
|
| 215 |
-
"graph_api": "Create and manage worksheets, tables, charts, and workbooks."
|
| 216 |
-
},
|
| 217 |
-
"π Word": {
|
| 218 |
-
"ai_capabilities": "Copilot for document drafting, summarization, and grammar improvements.",
|
| 219 |
-
"graph_api": "Document content access, templates, and file management."
|
| 220 |
-
},
|
| 221 |
-
"ποΈ SharePoint": {
|
| 222 |
-
"ai_capabilities": "Intelligent search, document tagging, and metadata extraction.",
|
| 223 |
-
"graph_api": "Access to sites, lists, files, and document libraries."
|
| 224 |
-
},
|
| 225 |
-
"π
Teams": {
|
| 226 |
-
"ai_capabilities": "Copilot for meeting summaries, transcription, and chat suggestions.",
|
| 227 |
-
"graph_api": "Manage chats, teams, channels, and meetings."
|
| 228 |
-
},
|
| 229 |
-
"π¬ Viva": {
|
| 230 |
-
"ai_capabilities": "Personalized learning insights, well-being insights, and productivity suggestions.",
|
| 231 |
-
"graph_api": "Access to user analytics and learning modules."
|
| 232 |
-
},
|
| 233 |
-
"π Power Platform": {
|
| 234 |
-
"ai_capabilities": "Automation with AI Builder, data insights, and custom AI models.",
|
| 235 |
-
"graph_api": "Automation workflows, app creation, and data visualization."
|
| 236 |
-
},
|
| 237 |
-
"π§ Copilot": {
|
| 238 |
-
"ai_capabilities": "Embedded across Word, Excel, Outlook, Teams, and more for AI-driven productivity.",
|
| 239 |
-
"graph_api": "Underpins Copilot's access to data and integrations."
|
| 240 |
-
},
|
| 241 |
-
"ποΈ OneDrive": {
|
| 242 |
-
"ai_capabilities": "Intelligent file organization and search.",
|
| 243 |
-
"graph_api": "File and folder access, sharing, and metadata."
|
| 244 |
-
},
|
| 245 |
-
"π‘ PowerPoint": {
|
| 246 |
-
"ai_capabilities": "Design suggestions, presentation summarization, and speaker coaching.",
|
| 247 |
-
"graph_api": "Presentation creation, slide management, and templates."
|
| 248 |
-
},
|
| 249 |
-
"π Microsoft Bookings": {
|
| 250 |
-
"ai_capabilities": "Automated scheduling and reminders.",
|
| 251 |
-
"graph_api": "Booking calendars, services, and appointment details."
|
| 252 |
-
},
|
| 253 |
-
"π Loop": {
|
| 254 |
-
"ai_capabilities": "Real-time collaboration and content suggestions.",
|
| 255 |
-
"graph_api": "Access to shared workspaces and collaborative pages."
|
| 256 |
-
},
|
| 257 |
-
"π£οΈ Translator": {
|
| 258 |
-
"ai_capabilities": "Real-time language translation and text-to-speech.",
|
| 259 |
-
"graph_api": "Integrated into communication and translation services."
|
| 260 |
-
},
|
| 261 |
-
"π To Do & Planner": {
|
| 262 |
-
"ai_capabilities": "Task prioritization and smart reminders.",
|
| 263 |
-
"graph_api": "Task creation, management, and synchronization."
|
| 264 |
-
},
|
| 265 |
-
"π Azure OpenAI Service": {
|
| 266 |
-
"ai_capabilities": "Access to GPT models for custom AI implementations.",
|
| 267 |
-
"graph_api": "Used indirectly for building custom AI models into workflows."
|
| 268 |
}
|
| 269 |
-
}
|
| 270 |
-
|
| 271 |
-
# π³οΈ Create toggles for each product
|
| 272 |
-
selected_products = {}
|
| 273 |
-
for product, info in products.items():
|
| 274 |
-
selected = st.sidebar.checkbox(product)
|
| 275 |
-
if selected:
|
| 276 |
-
st.sidebar.write(f"**AI Capabilities:** {info['ai_capabilities']}")
|
| 277 |
-
st.sidebar.write(f"**Graph API:** {info['graph_api']}")
|
| 278 |
-
selected_products[product] = True
|
| 279 |
-
|
| 280 |
-
# π Authentication
|
| 281 |
-
if 'access_token' not in st.session_state:
|
| 282 |
-
# π΅οΈββοΈ Check for authorization code in query parameters
|
| 283 |
-
query_params = st.query_params
|
| 284 |
-
query = (query_params.get('code'))
|
| 285 |
-
#if len(query) > 1:
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
st.session_state['access_token'] = access_token # π Save it for later
|
| 298 |
-
|
| 299 |
-
st.rerun() # Reload the app to clear the code from URL
|
| 300 |
-
else:
|
| 301 |
-
# π’ Prompt user to log in
|
| 302 |
-
client_instance = get_msal_app()
|
| 303 |
-
authorization_url = client_instance.get_authorization_request_url(
|
| 304 |
-
scopes=SCOPES,
|
| 305 |
-
redirect_uri=REDIRECT_URI
|
| 306 |
-
)
|
| 307 |
-
st.write('π Please [click here]({}) to log in and authorize the app.'.format(authorization_url))
|
| 308 |
-
st.stop()
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
else:
|
| 312 |
-
# π₯³ User is authenticated, proceed with the app
|
| 313 |
-
access_token = st.session_state['access_token']
|
| 314 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 315 |
-
|
| 316 |
-
# π€ Greet the user
|
| 317 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
|
| 318 |
-
if response.status_code == 200:
|
| 319 |
-
user_info = response.json()
|
| 320 |
-
st.sidebar.write(f"π Hello, {user_info['displayName']}!")
|
| 321 |
-
else:
|
| 322 |
-
st.error('Failed to fetch user info.')
|
| 323 |
-
st.write(response.text)
|
| 324 |
-
|
| 325 |
-
# ποΈ Handle menu options
|
| 326 |
-
if menu == "1οΈβ£ Dashboard with Widgets":
|
| 327 |
-
st.header("1οΈβ£ Dashboard with Widgets")
|
| 328 |
-
st.write("Widgets will be displayed here. ποΈ")
|
| 329 |
-
# Add your widgets here
|
| 330 |
-
|
| 331 |
-
elif menu == "π Landing Page":
|
| 332 |
-
st.header("π Landing Page")
|
| 333 |
-
st.write("Welcome to the app! π₯³")
|
| 334 |
-
# Add landing page content here
|
| 335 |
-
|
| 336 |
-
elif menu == "π
Upcoming Events":
|
| 337 |
-
st.header("π
Upcoming Events")
|
| 338 |
-
events = get_upcoming_events(access_token)
|
| 339 |
-
for event in events:
|
| 340 |
-
st.write(f"π {event['subject']} on {event['start']['dateTime']}")
|
| 341 |
-
# Display upcoming events
|
| 342 |
-
|
| 343 |
-
elif menu == "π Schedule":
|
| 344 |
-
st.header("π Schedule")
|
| 345 |
-
schedule = get_schedule(access_token)
|
| 346 |
-
st.write(schedule)
|
| 347 |
-
# Display schedule
|
| 348 |
-
|
| 349 |
-
elif menu == "π Agenda":
|
| 350 |
-
st.header("π Agenda")
|
| 351 |
-
st.write("Your agenda for today. π")
|
| 352 |
-
# Display agenda
|
| 353 |
-
|
| 354 |
-
elif menu == "π Event Details":
|
| 355 |
-
st.header("π Event Details")
|
| 356 |
-
event_id = st.text_input("Enter Event ID")
|
| 357 |
-
if event_id:
|
| 358 |
-
event_details = get_event_details(access_token, event_id)
|
| 359 |
-
st.write(event_details)
|
| 360 |
-
# Display event details based on ID
|
| 361 |
-
|
| 362 |
-
elif menu == "β Add Event":
|
| 363 |
-
st.header("β Add Event")
|
| 364 |
-
event_subject = st.text_input("Event Subject")
|
| 365 |
-
event_start = st.date_input("Event Start Date")
|
| 366 |
-
event_start_time = st.time_input("Event Start Time")
|
| 367 |
-
event_end = st.date_input("Event End Date")
|
| 368 |
-
event_end_time = st.time_input("Event End Time")
|
| 369 |
if st.button("Add Event"):
|
| 370 |
-
|
| 371 |
-
|
|
|
|
|
|
|
|
|
|
| 372 |
"start": {
|
| 373 |
-
"dateTime":
|
| 374 |
"timeZone": "UTC"
|
| 375 |
},
|
| 376 |
"end": {
|
| 377 |
-
"dateTime":
|
| 378 |
"timeZone": "UTC"
|
| 379 |
}
|
| 380 |
}
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
filter_criteria = st.text_input("Enter filter criteria")
|
| 388 |
-
if filter_criteria:
|
| 389 |
-
filtered_events = filter_events(access_token, filter_criteria)
|
| 390 |
-
for event in filtered_events:
|
| 391 |
-
st.write(f"π
{event['subject']} on {event['start']['dateTime']}")
|
| 392 |
-
# Filter events based on criteria
|
| 393 |
-
|
| 394 |
-
# π§© Handle selected products
|
| 395 |
-
if selected_products:
|
| 396 |
-
st.header("π§© M365 Product Integrations")
|
| 397 |
-
for product in selected_products:
|
| 398 |
-
st.subheader(f"{product}")
|
| 399 |
-
# Call the function corresponding to the product
|
| 400 |
-
handle_product_integration(access_token, product)
|
| 401 |
-
else:
|
| 402 |
-
st.write("No products selected.")
|
| 403 |
-
pass
|
| 404 |
-
|
| 405 |
-
# π§© Function to handle product integration
|
| 406 |
-
def handle_product_integration(access_token, product):
|
| 407 |
-
if product == "π§ Outlook":
|
| 408 |
-
st.write("Accessing Outlook data...")
|
| 409 |
-
# Implement Outlook integration
|
| 410 |
-
inbox_messages = get_outlook_messages(access_token)
|
| 411 |
-
st.write("Inbox Messages:")
|
| 412 |
-
for msg in inbox_messages:
|
| 413 |
-
st.write(f"βοΈ {msg['subject']}")
|
| 414 |
-
elif product == "π OneNote":
|
| 415 |
-
st.write("Accessing OneNote data...")
|
| 416 |
-
# Implement OneNote integration
|
| 417 |
-
notebooks = get_onenote_notebooks(access_token)
|
| 418 |
-
st.write("Notebooks:")
|
| 419 |
-
for notebook in notebooks:
|
| 420 |
-
st.write(f"π {notebook['displayName']}")
|
| 421 |
-
elif product == "π Excel":
|
| 422 |
-
st.write("Accessing Excel data...")
|
| 423 |
-
# Implement Excel integration
|
| 424 |
-
st.write("Excel integration is a placeholder.")
|
| 425 |
-
elif product == "π Word":
|
| 426 |
-
st.write("Accessing Word documents...")
|
| 427 |
-
# Implement Word integration
|
| 428 |
-
st.write("Word integration is a placeholder.")
|
| 429 |
-
elif product == "ποΈ SharePoint":
|
| 430 |
-
st.write("Accessing SharePoint sites...")
|
| 431 |
-
# Implement SharePoint integration
|
| 432 |
-
sites = get_sharepoint_sites(access_token)
|
| 433 |
-
st.write("Sites:")
|
| 434 |
-
for site in sites:
|
| 435 |
-
st.write(f"π {site['displayName']}")
|
| 436 |
-
elif product == "π
Teams":
|
| 437 |
-
st.write("Accessing Teams data...")
|
| 438 |
-
# Implement Teams integration
|
| 439 |
-
teams = get_user_teams(access_token)
|
| 440 |
-
st.write("Teams:")
|
| 441 |
-
for team in teams:
|
| 442 |
-
st.write(f"π₯ {team['displayName']}")
|
| 443 |
-
# Add additional product integrations as needed
|
| 444 |
-
else:
|
| 445 |
-
st.write(f"No integration implemented for {product}.")
|
| 446 |
-
|
| 447 |
-
# π¨ Function to get Outlook messages
|
| 448 |
-
def get_outlook_messages(access_token):
|
| 449 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 450 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me/messages?$top=5', headers=headers)
|
| 451 |
-
if response.status_code == 200:
|
| 452 |
-
messages = response.json().get('value', [])
|
| 453 |
-
return messages
|
| 454 |
-
else:
|
| 455 |
-
st.error('Failed to fetch messages.')
|
| 456 |
-
st.write(response.text)
|
| 457 |
-
return []
|
| 458 |
-
|
| 459 |
-
# π Function to get OneNote notebooks
|
| 460 |
-
def get_onenote_notebooks(access_token):
|
| 461 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 462 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me/onenote/notebooks', headers=headers)
|
| 463 |
-
if response.status_code == 200:
|
| 464 |
-
notebooks = response.json().get('value', [])
|
| 465 |
-
return notebooks
|
| 466 |
-
else:
|
| 467 |
-
st.error('Failed to fetch notebooks.')
|
| 468 |
-
st.write(response.text)
|
| 469 |
-
return []
|
| 470 |
-
|
| 471 |
-
# π Function to get SharePoint sites
|
| 472 |
-
def get_sharepoint_sites(access_token):
|
| 473 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 474 |
-
response = requests.get('https://graph.microsoft.com/v1.0/sites?search=*', headers=headers)
|
| 475 |
-
if response.status_code == 200:
|
| 476 |
-
sites = response.json().get('value', [])
|
| 477 |
-
return sites
|
| 478 |
-
else:
|
| 479 |
-
st.error('Failed to fetch sites.')
|
| 480 |
-
st.write(response.text)
|
| 481 |
-
return []
|
| 482 |
-
|
| 483 |
-
# π₯ Function to get user's Teams
|
| 484 |
-
def get_user_teams(access_token):
|
| 485 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 486 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me/joinedTeams', headers=headers)
|
| 487 |
-
if response.status_code == 200:
|
| 488 |
-
teams = response.json().get('value', [])
|
| 489 |
-
return teams
|
| 490 |
-
else:
|
| 491 |
-
st.error('Failed to fetch teams.')
|
| 492 |
-
st.write(response.text)
|
| 493 |
-
return []
|
| 494 |
-
|
| 495 |
-
# π
Function to get upcoming events
|
| 496 |
-
def get_upcoming_events(access_token):
|
| 497 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 498 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me/events?$orderby=start/dateTime&$top=10', headers=headers)
|
| 499 |
-
if response.status_code == 200:
|
| 500 |
-
events = response.json().get('value', [])
|
| 501 |
-
return events
|
| 502 |
-
else:
|
| 503 |
-
st.error('Failed to fetch upcoming events.')
|
| 504 |
-
st.write(response.text)
|
| 505 |
-
return []
|
| 506 |
-
|
| 507 |
-
# π Function to get schedule (Placeholder)
|
| 508 |
-
def get_schedule(access_token):
|
| 509 |
-
# Implement API call to get schedule
|
| 510 |
-
return "π Your schedule goes here."
|
| 511 |
-
|
| 512 |
-
# β Function to add a new event
|
| 513 |
-
def add_event(access_token, event_details):
|
| 514 |
-
headers = {
|
| 515 |
-
'Authorization': 'Bearer ' + access_token,
|
| 516 |
-
'Content-Type': 'application/json'
|
| 517 |
-
}
|
| 518 |
-
response = requests.post('https://graph.microsoft.com/v1.0/me/events', headers=headers, json=event_details)
|
| 519 |
-
if response.status_code == 201:
|
| 520 |
-
st.success('Event created successfully! π')
|
| 521 |
-
else:
|
| 522 |
-
st.error('Failed to create event.')
|
| 523 |
-
st.write(response.text)
|
| 524 |
-
|
| 525 |
-
# π Function to get event details
|
| 526 |
-
def get_event_details(access_token, event_id):
|
| 527 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 528 |
-
response = requests.get(f'https://graph.microsoft.com/v1.0/me/events/{event_id}', headers=headers)
|
| 529 |
-
if response.status_code == 200:
|
| 530 |
-
event = response.json()
|
| 531 |
-
return event
|
| 532 |
-
else:
|
| 533 |
-
st.error('Failed to fetch event details.')
|
| 534 |
-
st.write(response.text)
|
| 535 |
-
return {}
|
| 536 |
-
|
| 537 |
-
# π Function to filter events
|
| 538 |
-
def filter_events(access_token, filter_criteria):
|
| 539 |
-
headers = {'Authorization': 'Bearer ' + access_token}
|
| 540 |
-
# Implement filtering logic based on criteria
|
| 541 |
-
response = requests.get(f"https://graph.microsoft.com/v1.0/me/events?$filter=startswith(subject,'{filter_criteria}')", headers=headers)
|
| 542 |
-
if response.status_code == 200:
|
| 543 |
-
events = response.json().get('value', [])
|
| 544 |
-
return events
|
| 545 |
-
else:
|
| 546 |
-
st.error('Failed to filter events.')
|
| 547 |
-
st.write(response.text)
|
| 548 |
-
return []
|
| 549 |
|
| 550 |
-
# π Run the main function
|
| 551 |
if __name__ == "__main__":
|
| 552 |
-
main()
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import msal
|
|
|
|
|
|
|
|
|
|
| 5 |
import secrets
|
| 6 |
+
from urllib.parse import urlencode
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
APPLICATION_ID = os.getenv('APPLICATION_ID_KEY')
|
| 11 |
+
CLIENT_SECRET = os.getenv('CLIENT_SECRET_KEY')
|
| 12 |
+
AUTHORITY = 'https://login.microsoftonline.com/common'
|
| 13 |
+
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 14 |
+
SCOPES = ['User.Read', 'Calendars.ReadWrite', 'Mail.ReadWrite', 'Files.ReadWrite.All']
|
| 15 |
+
|
| 16 |
+
# MSAL setup
|
| 17 |
+
def get_msal_app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return msal.ConfidentialClientApplication(
|
| 19 |
+
client_id=APPLICATION_ID,
|
| 20 |
+
client_credential=CLIENT_SECRET,
|
| 21 |
+
authority=AUTHORITY
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Authentication functions
|
| 25 |
+
def generate_auth_url():
|
| 26 |
+
msal_app = get_msal_app()
|
| 27 |
+
state = secrets.token_urlsafe(32)
|
| 28 |
+
auth_url = msal_app.get_authorization_request_url(
|
| 29 |
+
scopes=SCOPES,
|
| 30 |
+
redirect_uri=REDIRECT_URI,
|
| 31 |
+
state=state
|
| 32 |
+
)
|
| 33 |
+
new_query_params = st.query_params.to_dict()
|
| 34 |
+
new_query_params['auth_state'] = state
|
| 35 |
+
return f"{auth_url}&{urlencode(new_query_params)}"
|
| 36 |
+
|
| 37 |
+
def get_token_from_code(code):
|
| 38 |
+
msal_app = get_msal_app()
|
| 39 |
+
result = msal_app.acquire_token_by_authorization_code(
|
| 40 |
+
code=code,
|
| 41 |
+
scopes=SCOPES,
|
| 42 |
+
redirect_uri=REDIRECT_URI
|
| 43 |
+
)
|
| 44 |
+
if 'access_token' in result:
|
| 45 |
+
return result
|
| 46 |
+
else:
|
| 47 |
+
raise Exception(f"Error acquiring token: {result.get('error_description')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# API call function
|
| 50 |
+
def make_api_call(endpoint, token, method='GET', data=None):
|
| 51 |
+
headers = {'Authorization': f'Bearer {token}', 'Content-Type': 'application/json'}
|
| 52 |
+
url = f'https://graph.microsoft.com/v1.0/{endpoint}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if method == 'GET':
|
| 55 |
+
response = requests.get(url, headers=headers)
|
| 56 |
+
elif method == 'POST':
|
| 57 |
+
response = requests.post(url, headers=headers, json=data)
|
| 58 |
+
else:
|
| 59 |
+
raise ValueError(f"Unsupported method: {method}")
|
|
|
|
| 60 |
|
| 61 |
+
if response.status_code in [200, 201]:
|
| 62 |
+
return response.json()
|
| 63 |
+
else:
|
| 64 |
+
st.error(f"API call failed: {response.status_code} - {response.text}")
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
# Product integration functions
|
| 68 |
+
def handle_outlook_integration(token):
|
| 69 |
+
st.subheader("π§ Outlook Integration")
|
| 70 |
+
emails = make_api_call('me/messages?$top=5', token)
|
| 71 |
+
if emails:
|
| 72 |
+
for email in emails['value']:
|
| 73 |
+
st.write(f"Subject: {email['subject']}")
|
| 74 |
+
st.write(f"From: {email['from']['emailAddress']['name']}")
|
| 75 |
+
st.write("---")
|
| 76 |
+
|
| 77 |
+
def handle_onenote_integration(token):
|
| 78 |
+
st.subheader("π OneNote Integration")
|
| 79 |
+
notebooks = make_api_call('me/onenote/notebooks', token)
|
| 80 |
+
if notebooks:
|
| 81 |
+
for notebook in notebooks['value']:
|
| 82 |
+
st.write(f"Notebook: {notebook['displayName']}")
|
| 83 |
+
|
| 84 |
+
def handle_calendar_integration(token):
|
| 85 |
+
st.subheader("π
Calendar Integration")
|
| 86 |
+
events = make_api_call('me/events?$top=5', token)
|
| 87 |
+
if events:
|
| 88 |
+
for event in events['value']:
|
| 89 |
+
st.write(f"Event: {event['subject']}")
|
| 90 |
+
st.write(f"Start: {event['start']['dateTime']}")
|
| 91 |
+
st.write("---")
|
| 92 |
+
|
| 93 |
+
def handle_onedrive_integration(token):
|
| 94 |
+
st.subheader("ποΈ OneDrive Integration")
|
| 95 |
+
files = make_api_call('me/drive/root/children', token)
|
| 96 |
+
if files:
|
| 97 |
+
for file in files['value']:
|
| 98 |
+
st.write(f"File: {file['name']}")
|
| 99 |
+
st.write(f"Type: {file['file']['mimeType'] if 'file' in file else 'Folder'}")
|
| 100 |
+
st.write("---")
|
| 101 |
+
|
| 102 |
+
# Main application
|
| 103 |
def main():
|
| 104 |
+
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 105 |
+
|
| 106 |
+
# Debug information
|
| 107 |
+
st.sidebar.write("Debug Info:")
|
| 108 |
+
st.sidebar.write(f"Query Params: {st.query_params.to_dict()}")
|
| 109 |
+
|
| 110 |
+
if 'code' in st.query_params and 'state' in st.query_params:
|
| 111 |
+
received_state = st.query_params['state']
|
| 112 |
+
expected_state = st.query_params.get('auth_state')
|
| 113 |
+
|
| 114 |
+
if received_state != expected_state:
|
| 115 |
+
st.error(f"Invalid state parameter. Expected {expected_state}, got {received_state}")
|
| 116 |
+
st.error("Please try logging in again.")
|
| 117 |
+
st.query_params.clear()
|
| 118 |
+
st.rerun()
|
| 119 |
+
|
| 120 |
+
try:
|
| 121 |
+
token = get_token_from_code(st.query_params['code'])
|
| 122 |
+
st.session_state['token'] = token
|
| 123 |
+
st.query_params.clear()
|
| 124 |
+
st.success("Successfully authenticated!")
|
| 125 |
+
st.rerun()
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"Authentication failed: {str(e)}")
|
| 128 |
+
st.query_params.clear()
|
| 129 |
+
st.rerun()
|
| 130 |
+
|
| 131 |
+
if 'token' not in st.session_state:
|
| 132 |
+
auth_url = generate_auth_url()
|
| 133 |
+
st.write("Please log in to continue:")
|
| 134 |
+
st.markdown(f"[Login with Microsoft]({auth_url})")
|
| 135 |
+
return
|
| 136 |
+
|
| 137 |
+
# User is authenticated, show the main app
|
| 138 |
+
token = st.session_state['token']['access_token']
|
| 139 |
+
st.sidebar.success("Authenticated successfully!")
|
| 140 |
|
| 141 |
+
# Display user info
|
| 142 |
+
user_info = make_api_call('me', token)
|
| 143 |
+
if user_info:
|
| 144 |
+
st.sidebar.write(f"Welcome, {user_info.get('displayName', 'User')}!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# App navigation
|
|
|
|
| 147 |
st.sidebar.title("Navigation")
|
| 148 |
+
app_mode = st.sidebar.selectbox("Choose the app mode",
|
| 149 |
+
["Dashboard", "Product Integration", "Event Management"])
|
| 150 |
+
|
| 151 |
+
if app_mode == "Dashboard":
|
| 152 |
+
st.header("π Dashboard")
|
| 153 |
+
# Add dashboard widgets here
|
| 154 |
+
|
| 155 |
+
elif app_mode == "Product Integration":
|
| 156 |
+
st.header("π§© Product Integration")
|
| 157 |
+
products = {
|
| 158 |
+
"π§ Outlook": handle_outlook_integration,
|
| 159 |
+
"π OneNote": handle_onenote_integration,
|
| 160 |
+
"π
Calendar": handle_calendar_integration,
|
| 161 |
+
"ποΈ OneDrive": handle_onedrive_integration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
for product, handler in products.items():
|
| 165 |
+
if st.checkbox(f"Enable {product}"):
|
| 166 |
+
handler(token)
|
| 167 |
+
|
| 168 |
+
elif app_mode == "Event Management":
|
| 169 |
+
st.header("π
Event Management")
|
| 170 |
+
event_action = st.radio("Choose an action", ["View Upcoming Events", "Add New Event"])
|
| 171 |
+
|
| 172 |
+
if event_action == "View Upcoming Events":
|
| 173 |
+
events = make_api_call('me/events?$top=10&$orderby=start/dateTime', token)
|
| 174 |
+
if events:
|
| 175 |
+
for event in events['value']:
|
| 176 |
+
st.write(f"Event: {event['subject']}")
|
| 177 |
+
st.write(f"Start: {event['start']['dateTime']}")
|
| 178 |
+
st.write("---")
|
| 179 |
+
|
| 180 |
+
elif event_action == "Add New Event":
|
| 181 |
+
subject = st.text_input("Event Subject")
|
| 182 |
+
start_date = st.date_input("Start Date")
|
| 183 |
+
start_time = st.time_input("Start Time")
|
| 184 |
+
duration = st.number_input("Duration (hours)", min_value=0.5, max_value=8.0, step=0.5)
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
if st.button("Add Event"):
|
| 187 |
+
start_datetime = datetime.combine(start_date, start_time)
|
| 188 |
+
end_datetime = start_datetime + timedelta(hours=duration)
|
| 189 |
+
|
| 190 |
+
event_data = {
|
| 191 |
+
"subject": subject,
|
| 192 |
"start": {
|
| 193 |
+
"dateTime": start_datetime.isoformat(),
|
| 194 |
"timeZone": "UTC"
|
| 195 |
},
|
| 196 |
"end": {
|
| 197 |
+
"dateTime": end_datetime.isoformat(),
|
| 198 |
"timeZone": "UTC"
|
| 199 |
}
|
| 200 |
}
|
| 201 |
+
|
| 202 |
+
result = make_api_call('me/events', token, method='POST', data=event_data)
|
| 203 |
+
if result:
|
| 204 |
+
st.success("Event added successfully!")
|
| 205 |
+
else:
|
| 206 |
+
st.error("Failed to add event.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
|
|
|
| 208 |
if __name__ == "__main__":
|
| 209 |
+
main()
|