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|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import msal
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import calendar
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY')
|
| 10 |
+
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY')
|
| 11 |
+
AUTHORITY_URL = 'https://login.microsoftonline.com/common'
|
| 12 |
+
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 13 |
+
|
| 14 |
+
# Define product to scope mapping, links, AI capabilities, and Graph solutions
|
| 15 |
+
PRODUCT_SCOPES = {
|
| 16 |
+
"π§ Outlook": {
|
| 17 |
+
'scopes': ['Mail.Read', 'Mail.Send'],
|
| 18 |
+
'link': 'https://outlook.office.com/mail/',
|
| 19 |
+
'ai_capabilities': "π€βοΈ Smart email & scheduling",
|
| 20 |
+
'graph_solution': "π¨π
Mail, calendar & contacts API"
|
| 21 |
+
},
|
| 22 |
+
"π
Calendar": {
|
| 23 |
+
'scopes': ['Calendars.ReadWrite'],
|
| 24 |
+
'link': 'https://outlook.office.com/calendar/',
|
| 25 |
+
'ai_capabilities': "π€π
Smart scheduling & reminders",
|
| 26 |
+
'graph_solution': "π
Calendar management API"
|
| 27 |
+
},
|
| 28 |
+
"π Tasks": {
|
| 29 |
+
'scopes': ['Tasks.ReadWrite'],
|
| 30 |
+
'link': 'https://to-do.office.com/tasks/',
|
| 31 |
+
'ai_capabilities': "π€π Task prioritization",
|
| 32 |
+
'graph_solution': "β
Task management API"
|
| 33 |
+
},
|
| 34 |
+
"ποΈ OneDrive": {
|
| 35 |
+
'scopes': ['Files.ReadWrite.All'],
|
| 36 |
+
'link': 'https://onedrive.live.com/',
|
| 37 |
+
'ai_capabilities': "π€π Smart file organization",
|
| 38 |
+
'graph_solution': "π File & folder API"
|
| 39 |
+
},
|
| 40 |
+
"π OneNote": {
|
| 41 |
+
'scopes': ['Notes.Read', 'Notes.Create'],
|
| 42 |
+
'link': 'https://www.onenote.com/notebooks',
|
| 43 |
+
'ai_capabilities': "π€π Content suggestion & OCR",
|
| 44 |
+
'graph_solution': "π Notebook & page API"
|
| 45 |
+
},
|
| 46 |
+
"π Excel": {
|
| 47 |
+
'scopes': ['Files.ReadWrite.All'],
|
| 48 |
+
'link': 'https://www.office.com/launch/excel',
|
| 49 |
+
'ai_capabilities': "π€π Data analysis & insights",
|
| 50 |
+
'graph_solution': "π Workbook & chart API"
|
| 51 |
+
},
|
| 52 |
+
"π Word": {
|
| 53 |
+
'scopes': ['Files.ReadWrite.All'],
|
| 54 |
+
'link': 'https://www.office.com/launch/word',
|
| 55 |
+
'ai_capabilities': "π€βοΈ Smart drafting & editing",
|
| 56 |
+
'graph_solution': "π Document content API"
|
| 57 |
+
},
|
| 58 |
+
"ποΈ SharePoint": {
|
| 59 |
+
'scopes': ['Sites.Read.All', 'Sites.ReadWrite.All'],
|
| 60 |
+
'link': 'https://www.microsoft.com/microsoft-365/sharepoint/collaboration',
|
| 61 |
+
'ai_capabilities': "π€π Smart search & tagging",
|
| 62 |
+
'graph_solution': "π Sites & lists API"
|
| 63 |
+
},
|
| 64 |
+
"π
Teams": {
|
| 65 |
+
'scopes': ['Team.ReadBasic.All', 'Channel.ReadBasic.All'],
|
| 66 |
+
'link': 'https://teams.microsoft.com/',
|
| 67 |
+
'ai_capabilities': "π€π¬ Meeting insights & summaries",
|
| 68 |
+
'graph_solution': "π₯ Teams & chats API"
|
| 69 |
+
},
|
| 70 |
+
"π¬ Viva": {
|
| 71 |
+
'scopes': ['Analytics.Read'],
|
| 72 |
+
'link': 'https://www.microsoft.com/microsoft-viva',
|
| 73 |
+
'ai_capabilities': "π€π Personalized insights",
|
| 74 |
+
'graph_solution': "π Analytics & learning API"
|
| 75 |
+
},
|
| 76 |
+
"π Power Platform": {
|
| 77 |
+
'scopes': ['Flow.Read.All'],
|
| 78 |
+
'link': 'https://powerplatform.microsoft.com/',
|
| 79 |
+
'ai_capabilities': "π€βοΈ AI-powered automation",
|
| 80 |
+
'graph_solution': "π§ Workflow & app API"
|
| 81 |
+
},
|
| 82 |
+
"π§ Copilot": {
|
| 83 |
+
'scopes': ['Cognitive.Read'],
|
| 84 |
+
'link': 'https://www.microsoft.com/microsoft-365/copilot',
|
| 85 |
+
'ai_capabilities': "π€π Cross-app AI assistance",
|
| 86 |
+
'graph_solution': "π§ AI integration API"
|
| 87 |
+
},
|
| 88 |
+
"π‘ PowerPoint": {
|
| 89 |
+
'scopes': ['Files.ReadWrite.All'],
|
| 90 |
+
'link': 'https://www.office.com/launch/powerpoint',
|
| 91 |
+
'ai_capabilities': "π€π¨ Design & coaching AI",
|
| 92 |
+
'graph_solution': "π Presentation API"
|
| 93 |
+
},
|
| 94 |
+
"π Microsoft Bookings": {
|
| 95 |
+
'scopes': ['Bookings.Read.All', 'Bookings.ReadWrite.All'],
|
| 96 |
+
'link': 'https://outlook.office.com/bookings/',
|
| 97 |
+
'ai_capabilities': "π€π
Smart scheduling",
|
| 98 |
+
'graph_solution': "π Booking services API"
|
| 99 |
+
},
|
| 100 |
+
"π Loop": {
|
| 101 |
+
'scopes': ['Files.ReadWrite.All'],
|
| 102 |
+
'link': 'https://loop.microsoft.com/',
|
| 103 |
+
'ai_capabilities': "π€π Real-time collaboration AI",
|
| 104 |
+
'graph_solution': "π Workspace API"
|
| 105 |
+
},
|
| 106 |
+
"π£οΈ Translator": {
|
| 107 |
+
'scopes': ['Translation.Read'],
|
| 108 |
+
'link': 'https://www.microsoft.com/translator/',
|
| 109 |
+
'ai_capabilities': "π€π Real-time translation",
|
| 110 |
+
'graph_solution': "π¨οΈ Translation services API"
|
| 111 |
+
},
|
| 112 |
+
"π To Do & Planner": {
|
| 113 |
+
'scopes': ['Tasks.ReadWrite'],
|
| 114 |
+
'link': 'https://todo.microsoft.com/',
|
| 115 |
+
'ai_capabilities': "π€π Smart task management",
|
| 116 |
+
'graph_solution': "β
Task & plan API"
|
| 117 |
+
},
|
| 118 |
+
"π Azure OpenAI Service": {
|
| 119 |
+
'scopes': ['AzureAIServices.ReadWrite.All'],
|
| 120 |
+
'link': 'https://azure.microsoft.com/products/cognitive-services/openai-service/',
|
| 121 |
+
'ai_capabilities': "π€π§ Custom AI model access",
|
| 122 |
+
'graph_solution': "π AI model integration API"
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
BASE_SCOPES = ['User.Read']
|
| 127 |
+
|
| 128 |
+
def get_msal_app():
|
| 129 |
+
return msal.ConfidentialClientApplication(
|
| 130 |
+
client_id=APPLICATION_ID_KEY,
|
| 131 |
+
client_credential=CLIENT_SECRET_KEY,
|
| 132 |
+
authority=AUTHORITY_URL
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
def get_access_token(code):
|
| 136 |
+
client_instance = get_msal_app()
|
| 137 |
+
try:
|
| 138 |
+
result = client_instance.acquire_token_by_authorization_code(
|
| 139 |
+
code=code,
|
| 140 |
+
scopes=st.session_state.get('request_scopes', BASE_SCOPES),
|
| 141 |
+
redirect_uri=REDIRECT_URI
|
| 142 |
+
)
|
| 143 |
+
if 'access_token' in result:
|
| 144 |
+
return result['access_token']
|
| 145 |
+
else:
|
| 146 |
+
raise Exception(f"Error acquiring token: {result.get('error_description')}")
|
| 147 |
+
except Exception as e:
|
| 148 |
+
st.error(f"Exception in get_access_token: {str(e)}")
|
| 149 |
+
raise
|
| 150 |
+
|
| 151 |
+
def make_api_call(access_token, endpoint, method='GET', data=None):
|
| 152 |
+
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
|
| 153 |
+
url = f'https://graph.microsoft.com/v1.0/{endpoint}'
|
| 154 |
+
|
| 155 |
+
if method == 'GET':
|
| 156 |
+
response = requests.get(url, headers=headers)
|
| 157 |
+
elif method == 'POST':
|
| 158 |
+
response = requests.post(url, headers=headers, json=data)
|
| 159 |
+
else:
|
| 160 |
+
raise ValueError(f"Unsupported method: {method}")
|
| 161 |
+
|
| 162 |
+
if response.status_code in [200, 201]:
|
| 163 |
+
return response.json()
|
| 164 |
+
else:
|
| 165 |
+
st.error(f"API call failed: {response.status_code} - {response.text}")
|
| 166 |
+
return None
|
| 167 |
+
|
| 168 |
+
def handle_outlook_integration(access_token):
|
| 169 |
+
st.subheader("π§ Outlook Integration")
|
| 170 |
+
st.markdown(f"[Open Outlook]({PRODUCT_SCOPES['π§ Outlook']['link']})")
|
| 171 |
+
|
| 172 |
+
# Read emails
|
| 173 |
+
emails = make_api_call(access_token, 'me/messages?$top=10&$orderby=receivedDateTime desc')
|
| 174 |
+
if emails and 'value' in emails:
|
| 175 |
+
for email in emails['value']:
|
| 176 |
+
with st.expander(f"From: {email['from']['emailAddress']['name']} - Subject: {email['subject']}"):
|
| 177 |
+
st.write(f"Received: {email['receivedDateTime']}")
|
| 178 |
+
st.write(f"Body: {email['bodyPreview']}")
|
| 179 |
+
else:
|
| 180 |
+
st.write("No emails found or unable to fetch emails.")
|
| 181 |
+
|
| 182 |
+
# Create (Send) email
|
| 183 |
+
st.write("Send a new email:")
|
| 184 |
+
recipient = st.text_input("Recipient Email")
|
| 185 |
+
subject = st.text_input("Subject")
|
| 186 |
+
body = st.text_area("Body")
|
| 187 |
+
if st.button("Send Email"):
|
| 188 |
+
new_email = {
|
| 189 |
+
"message": {
|
| 190 |
+
"subject": subject,
|
| 191 |
+
"body": {
|
| 192 |
+
"contentType": "Text",
|
| 193 |
+
"content": body
|
| 194 |
+
},
|
| 195 |
+
"toRecipients": [
|
| 196 |
+
{
|
| 197 |
+
"emailAddress": {
|
| 198 |
+
"address": recipient
|
| 199 |
+
}
|
| 200 |
+
}
|
| 201 |
+
]
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
result = make_api_call(access_token, 'me/sendMail', method='POST', data=new_email)
|
| 205 |
+
if result is None: # sendMail doesn't return content on success
|
| 206 |
+
st.success("Email sent successfully!")
|
| 207 |
+
else:
|
| 208 |
+
st.error("Failed to send email.")
|
| 209 |
+
|
| 210 |
+
# Update email (mark as read)
|
| 211 |
+
st.write("Mark an email as read:")
|
| 212 |
+
email_id = st.text_input("Enter Email ID")
|
| 213 |
+
if st.button("Mark as Read"):
|
| 214 |
+
update_data = {
|
| 215 |
+
"isRead": True
|
| 216 |
+
}
|
| 217 |
+
result = make_api_call(access_token, f'me/messages/{email_id}', method='PATCH', data=update_data)
|
| 218 |
+
if result is None: # PATCH doesn't return content on success
|
| 219 |
+
st.success("Email marked as read!")
|
| 220 |
+
else:
|
| 221 |
+
st.error("Failed to mark email as read.")
|
| 222 |
+
|
| 223 |
+
# Delete email
|
| 224 |
+
st.write("Delete an email:")
|
| 225 |
+
email_id = st.text_input("Enter Email ID to delete")
|
| 226 |
+
if st.button("Delete Email"):
|
| 227 |
+
result = make_api_call(access_token, f'me/messages/{email_id}', method='DELETE')
|
| 228 |
+
if result is None: # DELETE doesn't return content on success
|
| 229 |
+
st.success("Email deleted successfully!")
|
| 230 |
+
else:
|
| 231 |
+
st.error("Failed to delete email.")
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def handle_calendar_integration(access_token):
|
| 235 |
+
st.subheader("π
Calendar Integration")
|
| 236 |
+
st.markdown(f"[Open Calendar]({PRODUCT_SCOPES['π
Calendar']['link']})")
|
| 237 |
+
|
| 238 |
+
# Get the current month's start and end dates
|
| 239 |
+
now = datetime.now()
|
| 240 |
+
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 241 |
+
end_of_month = start_of_month.replace(month=start_of_month.month % 12 + 1, day=1) - timedelta(days=1)
|
| 242 |
+
|
| 243 |
+
events = make_api_call(access_token, f"me/calendarView?startDateTime={start_of_month.isoformat()}&endDateTime={end_of_month.isoformat()}&$orderby=start/dateTime")
|
| 244 |
+
|
| 245 |
+
if events and 'value' in events:
|
| 246 |
+
# Create a calendar view
|
| 247 |
+
cal = calendar.monthcalendar(now.year, now.month)
|
| 248 |
+
st.write(f"Calendar for {now.strftime('%B %Y')}")
|
| 249 |
+
|
| 250 |
+
# Create a placeholder for each day
|
| 251 |
+
day_placeholders = {}
|
| 252 |
+
for week in cal:
|
| 253 |
+
cols = st.columns(7)
|
| 254 |
+
for i, day in enumerate(week):
|
| 255 |
+
if day != 0:
|
| 256 |
+
day_placeholders[day] = cols[i].empty()
|
| 257 |
+
day_placeholders[day].write(f"**{day}**")
|
| 258 |
+
|
| 259 |
+
# Populate the calendar with events
|
| 260 |
+
for event in events['value']:
|
| 261 |
+
start_date = datetime.fromisoformat(event['start']['dateTime'][:-1]) # Remove 'Z' from the end
|
| 262 |
+
day = start_date.day
|
| 263 |
+
if day in day_placeholders:
|
| 264 |
+
day_placeholders[day].write(f"{start_date.strftime('%H:%M')} - {event['subject']}")
|
| 265 |
+
else:
|
| 266 |
+
st.write("No events found or unable to fetch events.")
|
| 267 |
+
|
| 268 |
+
# Create event
|
| 269 |
+
st.write("Add a new event:")
|
| 270 |
+
event_subject = st.text_input("Event Subject")
|
| 271 |
+
event_date = st.date_input("Event Date")
|
| 272 |
+
event_time = st.time_input("Event Time")
|
| 273 |
+
if st.button("Add Event"):
|
| 274 |
+
event_start = datetime.combine(event_date, event_time)
|
| 275 |
+
event_end = event_start + timedelta(hours=1)
|
| 276 |
+
new_event = {
|
| 277 |
+
"subject": event_subject,
|
| 278 |
+
"start": {
|
| 279 |
+
"dateTime": event_start.isoformat(),
|
| 280 |
+
"timeZone": "UTC"
|
| 281 |
+
},
|
| 282 |
+
"end": {
|
| 283 |
+
"dateTime": event_end.isoformat(),
|
| 284 |
+
"timeZone": "UTC"
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
result = make_api_call(access_token, 'me/events', method='POST', data=new_event)
|
| 288 |
+
if result:
|
| 289 |
+
st.success("Event added successfully!")
|
| 290 |
+
else:
|
| 291 |
+
st.error("Failed to add event.")
|
| 292 |
+
|
| 293 |
+
# Update event
|
| 294 |
+
st.write("Update an event:")
|
| 295 |
+
event_id = st.text_input("Enter Event ID")
|
| 296 |
+
new_subject = st.text_input("New Subject")
|
| 297 |
+
if st.button("Update Event"):
|
| 298 |
+
update_data = {
|
| 299 |
+
"subject": new_subject
|
| 300 |
+
}
|
| 301 |
+
result = make_api_call(access_token, f'me/events/{event_id}', method='PATCH', data=update_data)
|
| 302 |
+
if result is None: # PATCH doesn't return content on success
|
| 303 |
+
st.success("Event updated successfully!")
|
| 304 |
+
else:
|
| 305 |
+
st.error("Failed to update event.")
|
| 306 |
+
|
| 307 |
+
# Delete event
|
| 308 |
+
st.write("Delete an event:")
|
| 309 |
+
event_id_to_delete = st.text_input("Enter Event ID to delete")
|
| 310 |
+
if st.button("Delete Event"):
|
| 311 |
+
result = make_api_call(access_token, f'me/events/{event_id_to_delete}', method='DELETE')
|
| 312 |
+
if result is None: # DELETE doesn't return content on success
|
| 313 |
+
st.success("Event deleted successfully!")
|
| 314 |
+
else:
|
| 315 |
+
st.error("Failed to delete event.")
|
| 316 |
+
|
| 317 |
+
def handle_tasks_integration(access_token):
|
| 318 |
+
st.subheader("π Tasks Integration")
|
| 319 |
+
st.markdown(f"[Open Tasks]({PRODUCT_SCOPES['π Tasks']['link']})")
|
| 320 |
+
|
| 321 |
+
# Read tasks
|
| 322 |
+
tasks = make_api_call(access_token, 'me/todo/lists')
|
| 323 |
+
if tasks and 'value' in tasks:
|
| 324 |
+
default_list = next((list for list in tasks['value'] if list['wellknownListName'] == 'defaultList'), None)
|
| 325 |
+
if default_list:
|
| 326 |
+
tasks = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks")
|
| 327 |
+
if tasks and 'value' in tasks:
|
| 328 |
+
for task in tasks['value']:
|
| 329 |
+
st.write(f"Task: {task['title']}")
|
| 330 |
+
st.write(f"Status: {'Completed' if task['status'] == 'completed' else 'Not Completed'}")
|
| 331 |
+
st.write("---")
|
| 332 |
+
else:
|
| 333 |
+
st.write("No tasks found or unable to fetch tasks.")
|
| 334 |
+
else:
|
| 335 |
+
st.write("Default task list not found.")
|
| 336 |
+
else:
|
| 337 |
+
st.write("Unable to fetch task lists.")
|
| 338 |
+
|
| 339 |
+
# Create task
|
| 340 |
+
st.write("Add a new task:")
|
| 341 |
+
task_title = st.text_input("Task Title")
|
| 342 |
+
if st.button("Add Task"):
|
| 343 |
+
new_task = {
|
| 344 |
+
"title": task_title
|
| 345 |
+
}
|
| 346 |
+
result = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks", method='POST', data=new_task)
|
| 347 |
+
if result:
|
| 348 |
+
st.success("Task added successfully!")
|
| 349 |
+
else:
|
| 350 |
+
st.error("Failed to add task.")
|
| 351 |
+
|
| 352 |
+
# Update task
|
| 353 |
+
st.write("Update a task:")
|
| 354 |
+
task_id = st.text_input("Enter Task ID")
|
| 355 |
+
new_title = st.text_input("New Title")
|
| 356 |
+
if st.button("Update Task"):
|
| 357 |
+
update_data = {
|
| 358 |
+
"title": new_title
|
| 359 |
+
}
|
| 360 |
+
result = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks/{task_id}", method='PATCH', data=update_data)
|
| 361 |
+
if result is None: # PATCH doesn't return content on success
|
| 362 |
+
st.success("Task updated successfully!")
|
| 363 |
+
else:
|
| 364 |
+
st.error("Failed to update task.")
|
| 365 |
+
|
| 366 |
+
# Delete task
|
| 367 |
+
st.write("Delete a task:")
|
| 368 |
+
task_id_to_delete = st.text_input("Enter Task ID to delete")
|
| 369 |
+
if st.button("Delete Task"):
|
| 370 |
+
result = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks/{task_id_to_delete}", method='DELETE')
|
| 371 |
+
if result is None: # DELETE doesn't return content on success
|
| 372 |
+
st.success("Task deleted successfully!")
|
| 373 |
+
else:
|
| 374 |
+
st.error("Failed to delete task.")
|
| 375 |
+
|
| 376 |
+
def handle_onedrive_integration(access_token):
|
| 377 |
+
st.subheader("ποΈ OneDrive Integration")
|
| 378 |
+
st.markdown(f"[Open OneDrive]({PRODUCT_SCOPES['ποΈ OneDrive']['link']})")
|
| 379 |
+
|
| 380 |
+
# Read files
|
| 381 |
+
files = make_api_call(access_token, 'me/drive/root/children')
|
| 382 |
+
if files and 'value' in files:
|
| 383 |
+
for file in files['value']:
|
| 384 |
+
st.write(f"Name: {file['name']}")
|
| 385 |
+
st.write(f"Type: {'Folder' if 'folder' in file else 'File'}")
|
| 386 |
+
st.write(f"Last Modified: {file['lastModifiedDateTime']}")
|
| 387 |
+
st.write("---")
|
| 388 |
+
else:
|
| 389 |
+
st.write("No files found or unable to fetch files.")
|
| 390 |
+
|
| 391 |
+
# Create file
|
| 392 |
+
st.write("Create a new text file:")
|
| 393 |
+
file_name = st.text_input("File Name (include .txt extension)")
|
| 394 |
+
file_content = st.text_area("File Content")
|
| 395 |
+
if st.button("Create File"):
|
| 396 |
+
create_file_url = f"https://graph.microsoft.com/v1.0/me/drive/root:/{file_name}:/content"
|
| 397 |
+
headers = {
|
| 398 |
+
'Authorization': f'Bearer {access_token}',
|
| 399 |
+
'Content-Type': 'text/plain'
|
| 400 |
+
}
|
| 401 |
+
response = requests.put(create_file_url, headers=headers, data=file_content.encode('utf-8'))
|
| 402 |
+
if response.status_code == 201:
|
| 403 |
+
st.success("File created successfully!")
|
| 404 |
+
else:
|
| 405 |
+
st.error("Failed to create file.")
|
| 406 |
+
|
| 407 |
+
# Update file
|
| 408 |
+
st.write("Update a file:")
|
| 409 |
+
file_path = st.text_input("File Path (e.g., /Documents/file.txt)")
|
| 410 |
+
new_content = st.text_area("New Content")
|
| 411 |
+
if st.button("Update File"):
|
| 412 |
+
update_file_url = f"https://graph.microsoft.com/v1.0/me/drive/root:{file_path}:/content"
|
| 413 |
+
headers = {
|
| 414 |
+
'Authorization': f'Bearer {access_token}',
|
| 415 |
+
'Content-Type': 'text/plain'
|
| 416 |
+
}
|
| 417 |
+
response = requests.put(update_file_url, headers=headers, data=new_content.encode('utf-8'))
|
| 418 |
+
if response.status_code == 200:
|
| 419 |
+
st.success("File updated successfully!")
|
| 420 |
+
else:
|
| 421 |
+
st.error("Failed to update file.")
|
| 422 |
+
|
| 423 |
+
# Delete file
|
| 424 |
+
st.write("Delete a file:")
|
| 425 |
+
file_path_to_delete = st.text_input("File Path to delete (e.g., /Documents/file.txt)")
|
| 426 |
+
if st.button("Delete File"):
|
| 427 |
+
result = make_api_call(access_token, f"me/drive/root:{file_path_to_delete}", method='DELETE')
|
| 428 |
+
if result is None: # DELETE doesn't return content on success
|
| 429 |
+
st.success("File deleted successfully!")
|
| 430 |
+
else:
|
| 431 |
+
st.error("Failed to delete file.")
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def main():
|
| 437 |
+
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 438 |
+
|
| 439 |
+
st.sidebar.title("π M365 Products")
|
| 440 |
+
st.sidebar.write("Select products to integrate:")
|
| 441 |
+
|
| 442 |
+
selected_products = {}
|
| 443 |
+
for product, details in PRODUCT_SCOPES.items():
|
| 444 |
+
selected = st.sidebar.checkbox(product)
|
| 445 |
+
if selected:
|
| 446 |
+
selected_products[product] = True
|
| 447 |
+
st.sidebar.write(f"AI Capabilities: {details['ai_capabilities']}")
|
| 448 |
+
st.sidebar.write(f"Graph Solution: {details['graph_solution']}")
|
| 449 |
+
|
| 450 |
+
request_scopes = BASE_SCOPES.copy()
|
| 451 |
+
for product in selected_products:
|
| 452 |
+
request_scopes.extend(PRODUCT_SCOPES[product]['scopes'])
|
| 453 |
+
request_scopes = list(set(request_scopes))
|
| 454 |
+
|
| 455 |
+
st.session_state['request_scopes'] = request_scopes
|
| 456 |
+
|
| 457 |
+
if 'access_token' not in st.session_state:
|
| 458 |
+
client_instance = get_msal_app()
|
| 459 |
+
auth_url = client_instance.get_authorization_request_url(
|
| 460 |
+
scopes=request_scopes,
|
| 461 |
+
redirect_uri=REDIRECT_URI
|
| 462 |
+
)
|
| 463 |
+
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 464 |
+
|
| 465 |
+
query_params = st.query_params
|
| 466 |
+
if 'code' in query_params:
|
| 467 |
+
code = query_params.get('code')
|
| 468 |
+
st.write('π Authorization Code Obtained:', code[:10] + '...')
|
| 469 |
+
|
| 470 |
+
try:
|
| 471 |
+
access_token = get_access_token(code)
|
| 472 |
+
st.session_state['access_token'] = access_token
|
| 473 |
+
st.success("Access token acquired successfully!")
|
| 474 |
+
st.rerun()
|
| 475 |
+
except Exception as e:
|
| 476 |
+
st.error(f"Error acquiring access token: {str(e)}")
|
| 477 |
+
st.stop()
|
| 478 |
+
else:
|
| 479 |
+
access_token = st.session_state['access_token']
|
| 480 |
+
|
| 481 |
+
user_info = make_api_call(access_token, 'me')
|
| 482 |
+
if user_info:
|
| 483 |
+
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 484 |
+
|
| 485 |
+
if selected_products:
|
| 486 |
+
for product in selected_products:
|
| 487 |
+
if product == "π§ Outlook":
|
| 488 |
+
handle_outlook_integration(access_token)
|
| 489 |
+
elif product == "π
Calendar":
|
| 490 |
+
handle_calendar_integration(access_token)
|
| 491 |
+
elif product == "π Tasks":
|
| 492 |
+
handle_tasks_integration(access_token)
|
| 493 |
+
elif product == "ποΈ OneDrive":
|
| 494 |
+
handle_onedrive_integration(access_token)
|
| 495 |
+
# Add more product integrations here
|
| 496 |
+
else:
|
| 497 |
+
st.write("No products selected. Please select products from the sidebar.")
|
| 498 |
+
|
| 499 |
+
if __name__ == "__main__":
|
| 500 |
+
main()
|
| 501 |
+
|
| 502 |
+
|
| 503 |
+
|
| 504 |
+
|