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