Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,49 +11,120 @@ 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 and
|
| 15 |
PRODUCT_SCOPES = {
|
| 16 |
-
"π§ Outlook": ['Mail.Read', 'Mail.Send', 'Calendars.ReadWrite'],
|
| 17 |
-
"π OneNote": ['Notes.Read', 'Notes.Create'],
|
| 18 |
-
"π Excel": ['Files.ReadWrite.All'],
|
| 19 |
-
"π Word": ['Files.ReadWrite.All'],
|
| 20 |
-
"ποΈ SharePoint": ['Sites.Read.All', 'Sites.ReadWrite.All'],
|
| 21 |
-
"π
Teams": ['Team.ReadBasic.All', 'Channel.ReadBasic.All'],
|
| 22 |
-
"π¬ Viva": ['Analytics.Read'],
|
| 23 |
-
"π Power Platform": ['Flow.Read.All'],
|
| 24 |
-
"π§ Copilot": ['Cognitive.Read'],
|
| 25 |
-
"ποΈ OneDrive": ['Files.ReadWrite.All'],
|
| 26 |
-
"π‘ PowerPoint": ['Files.ReadWrite.All'],
|
| 27 |
-
"π Microsoft Bookings": ['Bookings.Read.All', 'Bookings.ReadWrite.All'],
|
| 28 |
-
"π Loop": ['Files.ReadWrite.All'],
|
| 29 |
-
"π£οΈ Translator": ['Translation.Read'],
|
| 30 |
-
"π To Do & Planner": ['Tasks.ReadWrite'],
|
| 31 |
-
"π Azure OpenAI Service": ['AzureAIServices.ReadWrite.All']
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
# Add links to the products dictionary
|
| 35 |
-
products = {
|
| 36 |
"π§ Outlook": {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
},
|
| 41 |
"π OneNote": {
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
| 45 |
},
|
| 46 |
"π Excel": {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
},
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
|
| 54 |
BASE_SCOPES = ['User.Read']
|
| 55 |
|
| 56 |
-
# MSAL App Instance
|
| 57 |
def get_msal_app():
|
| 58 |
return msal.ConfidentialClientApplication(
|
| 59 |
client_id=APPLICATION_ID_KEY,
|
|
@@ -61,7 +132,6 @@ def get_msal_app():
|
|
| 61 |
authority=AUTHORITY_URL
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# Get Access Token
|
| 65 |
def get_access_token(code):
|
| 66 |
client_instance = get_msal_app()
|
| 67 |
try:
|
|
@@ -78,7 +148,6 @@ def get_access_token(code):
|
|
| 78 |
st.error(f"Exception in get_access_token: {str(e)}")
|
| 79 |
raise
|
| 80 |
|
| 81 |
-
# Make API call
|
| 82 |
def make_api_call(access_token, endpoint, method='GET', data=None):
|
| 83 |
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json'}
|
| 84 |
url = f'https://graph.microsoft.com/v1.0/{endpoint}'
|
|
@@ -96,10 +165,10 @@ def make_api_call(access_token, endpoint, method='GET', data=None):
|
|
| 96 |
st.error(f"API call failed: {response.status_code} - {response.text}")
|
| 97 |
return None
|
| 98 |
|
| 99 |
-
# Define product integration handlers
|
| 100 |
def handle_outlook_integration(access_token):
|
| 101 |
st.subheader("π§ Outlook Integration")
|
| 102 |
-
st.markdown(f"[Open Outlook]({
|
|
|
|
| 103 |
emails = make_api_call(access_token, 'me/messages?$top=10&$orderby=receivedDateTime desc')
|
| 104 |
if emails and 'value' in emails:
|
| 105 |
for email in emails['value']:
|
|
@@ -109,44 +178,142 @@ def handle_outlook_integration(access_token):
|
|
| 109 |
else:
|
| 110 |
st.write("No emails found or unable to fetch emails.")
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
# Main Function
|
| 115 |
def main():
|
| 116 |
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 117 |
-
|
| 118 |
-
# Sidebar product selection
|
| 119 |
st.sidebar.title("π M365 Products")
|
| 120 |
st.sidebar.write("Select products to integrate:")
|
| 121 |
|
| 122 |
selected_products = {}
|
| 123 |
-
for product
|
| 124 |
selected = st.sidebar.checkbox(product)
|
| 125 |
if selected:
|
| 126 |
selected_products[product] = True
|
| 127 |
-
st.sidebar.write(f"**AI Capabilities:** {info['ai_capabilities']}")
|
| 128 |
-
st.sidebar.write(f"**Graph API:** {info['graph_api']}")
|
| 129 |
|
| 130 |
-
# Request scopes based on selected products
|
| 131 |
request_scopes = BASE_SCOPES.copy()
|
| 132 |
for product in selected_products:
|
| 133 |
-
request_scopes.extend(PRODUCT_SCOPES[product])
|
| 134 |
-
request_scopes = list(set(request_scopes))
|
|
|
|
| 135 |
st.session_state['request_scopes'] = request_scopes
|
| 136 |
|
| 137 |
-
# MSAL login and token handling
|
| 138 |
if 'access_token' not in st.session_state:
|
| 139 |
client_instance = get_msal_app()
|
| 140 |
auth_url = client_instance.get_authorization_request_url(
|
| 141 |
scopes=request_scopes,
|
| 142 |
redirect_uri=REDIRECT_URI
|
| 143 |
)
|
| 144 |
-
st.write(
|
| 145 |
|
| 146 |
query_params = st.query_params
|
| 147 |
if 'code' in query_params:
|
| 148 |
code = query_params.get('code')
|
| 149 |
-
st.write(
|
| 150 |
|
| 151 |
try:
|
| 152 |
access_token = get_access_token(code)
|
|
@@ -164,33 +331,19 @@ def main():
|
|
| 164 |
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 165 |
|
| 166 |
if selected_products:
|
| 167 |
-
# Integrate selected products
|
| 168 |
for product in selected_products:
|
| 169 |
if product == "π§ Outlook":
|
| 170 |
handle_outlook_integration(access_token)
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
else:
|
| 173 |
st.write("No products selected. Please select products from the sidebar.")
|
| 174 |
|
| 175 |
-
# Sidebar navigation menu with AI capabilities and Graph API descriptions
|
| 176 |
-
st.sidebar.title("Navigation")
|
| 177 |
-
menu = st.sidebar.radio("Go to", [
|
| 178 |
-
"1οΈβ£ Dashboard",
|
| 179 |
-
"π Landing Page",
|
| 180 |
-
"π
Upcoming Events",
|
| 181 |
-
"π Schedule",
|
| 182 |
-
"π Agenda",
|
| 183 |
-
"π Event Details",
|
| 184 |
-
"β Add Event",
|
| 185 |
-
"π Filter By"
|
| 186 |
-
])
|
| 187 |
-
|
| 188 |
-
# Display AI Capabilities and Graph API Information for the selected menu
|
| 189 |
-
if menu in selected_products:
|
| 190 |
-
product_info = products.get(menu, None)
|
| 191 |
-
if product_info:
|
| 192 |
-
st.write(f"**AI Capabilities for {menu}:** {product_info['ai_capabilities']}")
|
| 193 |
-
st.write(f"**Graph API for {menu}:** {product_info['graph_api']}")
|
| 194 |
-
|
| 195 |
if __name__ == "__main__":
|
| 196 |
main()
|
|
|
|
|
|
| 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,
|
|
|
|
| 132 |
authority=AUTHORITY_URL
|
| 133 |
)
|
| 134 |
|
|
|
|
| 135 |
def get_access_token(code):
|
| 136 |
client_instance = get_msal_app()
|
| 137 |
try:
|
|
|
|
| 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}'
|
|
|
|
| 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 |
emails = make_api_call(access_token, 'me/messages?$top=10&$orderby=receivedDateTime desc')
|
| 173 |
if emails and 'value' in emails:
|
| 174 |
for email in emails['value']:
|
|
|
|
| 178 |
else:
|
| 179 |
st.write("No emails found or unable to fetch emails.")
|
| 180 |
|
| 181 |
+
def handle_calendar_integration(access_token):
|
| 182 |
+
st.subheader("π
Calendar Integration")
|
| 183 |
+
st.markdown(f"[Open Calendar]({PRODUCT_SCOPES['π
Calendar']['link']})")
|
| 184 |
+
|
| 185 |
+
# Get the current month's start and end dates
|
| 186 |
+
now = datetime.now()
|
| 187 |
+
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
| 188 |
+
end_of_month = start_of_month.replace(month=start_of_month.month % 12 + 1, day=1) - timedelta(days=1)
|
| 189 |
+
|
| 190 |
+
events = make_api_call(access_token, f"me/calendarView?startDateTime={start_of_month.isoformat()}&endDateTime={end_of_month.isoformat()}&$orderby=start/dateTime")
|
| 191 |
+
|
| 192 |
+
if events and 'value' in events:
|
| 193 |
+
# Create a calendar view
|
| 194 |
+
cal = calendar.monthcalendar(now.year, now.month)
|
| 195 |
+
st.write(f"Calendar for {now.strftime('%B %Y')}")
|
| 196 |
+
|
| 197 |
+
# Create a placeholder for each day
|
| 198 |
+
day_placeholders = {}
|
| 199 |
+
for week in cal:
|
| 200 |
+
cols = st.columns(7)
|
| 201 |
+
for i, day in enumerate(week):
|
| 202 |
+
if day != 0:
|
| 203 |
+
day_placeholders[day] = cols[i].empty()
|
| 204 |
+
day_placeholders[day].write(f"**{day}**")
|
| 205 |
+
|
| 206 |
+
# Populate the calendar with events
|
| 207 |
+
for event in events['value']:
|
| 208 |
+
start_date = datetime.fromisoformat(event['start']['dateTime'][:-1]) # Remove 'Z' from the end
|
| 209 |
+
day = start_date.day
|
| 210 |
+
if day in day_placeholders:
|
| 211 |
+
day_placeholders[day].write(f"{start_date.strftime('%H:%M')} - {event['subject']}")
|
| 212 |
+
else:
|
| 213 |
+
st.write("No events found or unable to fetch events.")
|
| 214 |
+
|
| 215 |
+
st.write("Add a new event:")
|
| 216 |
+
event_subject = st.text_input("Event Subject")
|
| 217 |
+
event_date = st.date_input("Event Date")
|
| 218 |
+
event_time = st.time_input("Event Time")
|
| 219 |
+
if st.button("Add Event"):
|
| 220 |
+
event_start = datetime.combine(event_date, event_time)
|
| 221 |
+
event_end = event_start + timedelta(hours=1)
|
| 222 |
+
new_event = {
|
| 223 |
+
"subject": event_subject,
|
| 224 |
+
"start": {
|
| 225 |
+
"dateTime": event_start.isoformat(),
|
| 226 |
+
"timeZone": "UTC"
|
| 227 |
+
},
|
| 228 |
+
"end": {
|
| 229 |
+
"dateTime": event_end.isoformat(),
|
| 230 |
+
"timeZone": "UTC"
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
result = make_api_call(access_token, 'me/events', method='POST', data=new_event)
|
| 234 |
+
if result:
|
| 235 |
+
st.success("Event added successfully!")
|
| 236 |
+
else:
|
| 237 |
+
st.error("Failed to add event.")
|
| 238 |
+
|
| 239 |
+
def handle_tasks_integration(access_token):
|
| 240 |
+
st.subheader("π Tasks Integration")
|
| 241 |
+
st.markdown(f"[Open Tasks]({PRODUCT_SCOPES['π Tasks']['link']})")
|
| 242 |
+
|
| 243 |
+
tasks = make_api_call(access_token, 'me/todo/lists')
|
| 244 |
+
if tasks and 'value' in tasks:
|
| 245 |
+
default_list = next((list for list in tasks['value'] if list['wellknownListName'] == 'defaultList'), None)
|
| 246 |
+
if default_list:
|
| 247 |
+
tasks = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks")
|
| 248 |
+
if tasks and 'value' in tasks:
|
| 249 |
+
for task in tasks['value']:
|
| 250 |
+
st.write(f"Task: {task['title']}")
|
| 251 |
+
st.write(f"Status: {'Completed' if task['status'] == 'completed' else 'Not Completed'}")
|
| 252 |
+
st.write("---")
|
| 253 |
+
else:
|
| 254 |
+
st.write("No tasks found or unable to fetch tasks.")
|
| 255 |
+
else:
|
| 256 |
+
st.write("Default task list not found.")
|
| 257 |
+
else:
|
| 258 |
+
st.write("Unable to fetch task lists.")
|
| 259 |
+
|
| 260 |
+
st.write("Add a new task:")
|
| 261 |
+
task_title = st.text_input("Task Title")
|
| 262 |
+
if st.button("Add Task"):
|
| 263 |
+
new_task = {
|
| 264 |
+
"title": task_title
|
| 265 |
+
}
|
| 266 |
+
result = make_api_call(access_token, f"me/todo/lists/{default_list['id']}/tasks", method='POST', data=new_task)
|
| 267 |
+
if result:
|
| 268 |
+
st.success("Task added successfully!")
|
| 269 |
+
else:
|
| 270 |
+
st.error("Failed to add task.")
|
| 271 |
+
|
| 272 |
+
def handle_onedrive_integration(access_token):
|
| 273 |
+
st.subheader("ποΈ OneDrive Integration")
|
| 274 |
+
st.markdown(f"[Open OneDrive]({PRODUCT_SCOPES['ποΈ OneDrive']['link']})")
|
| 275 |
+
|
| 276 |
+
files = make_api_call(access_token, 'me/drive/root/children')
|
| 277 |
+
if files and 'value' in files:
|
| 278 |
+
for file in files['value']:
|
| 279 |
+
st.write(f"Name: {file['name']}")
|
| 280 |
+
st.write(f"Type: {'Folder' if 'folder' in file else 'File'}")
|
| 281 |
+
st.write(f"Last Modified: {file['lastModifiedDateTime']}")
|
| 282 |
+
st.write("---")
|
| 283 |
+
else:
|
| 284 |
+
st.write("No files found or unable to fetch files.")
|
| 285 |
|
|
|
|
| 286 |
def main():
|
| 287 |
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 288 |
+
|
|
|
|
| 289 |
st.sidebar.title("π M365 Products")
|
| 290 |
st.sidebar.write("Select products to integrate:")
|
| 291 |
|
| 292 |
selected_products = {}
|
| 293 |
+
for product in PRODUCT_SCOPES.keys():
|
| 294 |
selected = st.sidebar.checkbox(product)
|
| 295 |
if selected:
|
| 296 |
selected_products[product] = True
|
|
|
|
|
|
|
| 297 |
|
|
|
|
| 298 |
request_scopes = BASE_SCOPES.copy()
|
| 299 |
for product in selected_products:
|
| 300 |
+
request_scopes.extend(PRODUCT_SCOPES[product]['scopes'])
|
| 301 |
+
request_scopes = list(set(request_scopes))
|
| 302 |
+
|
| 303 |
st.session_state['request_scopes'] = request_scopes
|
| 304 |
|
|
|
|
| 305 |
if 'access_token' not in st.session_state:
|
| 306 |
client_instance = get_msal_app()
|
| 307 |
auth_url = client_instance.get_authorization_request_url(
|
| 308 |
scopes=request_scopes,
|
| 309 |
redirect_uri=REDIRECT_URI
|
| 310 |
)
|
| 311 |
+
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 312 |
|
| 313 |
query_params = st.query_params
|
| 314 |
if 'code' in query_params:
|
| 315 |
code = query_params.get('code')
|
| 316 |
+
st.write('π Authorization Code Obtained:', code[:10] + '...')
|
| 317 |
|
| 318 |
try:
|
| 319 |
access_token = get_access_token(code)
|
|
|
|
| 331 |
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 332 |
|
| 333 |
if selected_products:
|
|
|
|
| 334 |
for product in selected_products:
|
| 335 |
if product == "π§ Outlook":
|
| 336 |
handle_outlook_integration(access_token)
|
| 337 |
+
elif product == "π
Calendar":
|
| 338 |
+
handle_calendar_integration(access_token)
|
| 339 |
+
elif product == "π Tasks":
|
| 340 |
+
handle_tasks_integration(access_token)
|
| 341 |
+
elif product == "ποΈ OneDrive":
|
| 342 |
+
handle_onedrive_integration(access_token)
|
| 343 |
+
# Add more product integrations here
|
| 344 |
else:
|
| 345 |
st.write("No products selected. Please select products from the sidebar.")
|
| 346 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
if __name__ == "__main__":
|
| 348 |
main()
|
| 349 |
+
|