Spaces:
Running
Running
import msal | |
import os | |
import streamlit as st | |
import webbrowser as wb | |
import requests | |
from msal import PublicClientApplication | |
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY') | |
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY') | |
AUTHORIZATION_KEY = os.getenv('AUTHORIZATION_KEY') | |
#st.write(APPLICATION_ID_KEY) | |
#st.write(CLIENT_SECRET_KEY) | |
authority_url = 'https://login.microsoftonline.com/consumers' | |
base_url = 'https://graph.microsoft.com/v1.0/' | |
endpoint = base_url + 'me' | |
SCOPES = ['User.Read','User.Export.All'] | |
# Authenticate with Auth Code | |
client_instance = msal.ConfidentialClientApplication( | |
client_id=APPLICATION_ID_KEY, client_credential=CLIENT_SECRET_KEY, authority=authority_url | |
) | |
authorization_request_url = client_instance.get_authorization_request_url(SCOPES) | |
st.write('Connecting to MSGraph with url:' + authorization_request_url) | |
wb.open(authorization_request_url, new=True) | |
access_token = client_instance.acquire_token_by_authorization_code( | |
code=AUTHORIZATION_KEY, | |
scopes=SCOPES) | |
try: | |
access_token_id = access_token['access_token'] | |
headers = {'Authorization': 'Bearer ' + access_token_id} | |
endpoint = base_url + 'me' | |
response = requests.get(endpoint, headers=headers) | |
st.write(response) | |
st.write(response.json) | |
except: | |
st.write('No auth key returned from MS graph - redirect issue?') | |
# URLs used in this sample app to demonstrate MS Graph in Python | |
# https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/~/Authentication/appId/d36c689d-3c61-4be6-a230-c09fc54cf80d/objectId/ece93996-1d7c-4b39-abc5-de51487073ed/isMSAApp~/false/defaultBlade/Overview/appSignInAudience/AzureADandPersonalMicrosoftAccount/servicePrincipalCreated~/true | |
# https://developer.microsoft.com/en-us/graph/graph-explorer | |
# https://chatgpt.com/c/67065b64-e2a8-800d-8b64-69cfe7aaed7f | |
# https://www.google.com/search?q=msal+pypi&oq=msal+pypi&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIICAEQABgWGB4yCAgCEAAYFhgeMg0IAxAAGIYDGIAEGIoFMg0IBBAAGIYDGIAEGIoFMg0IBRAAGIYDGIAEGIoFMgoIBhAAGIAEGKIEMgoIBxAAGIAEGKIEMgoICBAAGIAEGKIE0gEIMTk2NGowajeoAgCwAgA&sourceid=chrome&ie=UTF-8 | |
# https://www.youtube.com/watch?v=1Jyd7SA-0kI&list=PLHgX2IExbFot3M2dudQEbTVEk9ikRJ2h5&t=972s | |
# https://huggingface.co/spaces/awacke1/MSGraphAPI | |
# Initialize MSAL client | |
def get_msal_app(): | |
return ConfidentialClientApplication( | |
CLIENT_ID, | |
authority=AUTHORITY_URL, | |
client_credential=CLIENT_SECRET | |
) | |
# Get access token | |
def get_access_token(): | |
app = get_msal_app() | |
result = app.acquire_token_silent(SCOPES, account=None) | |
if not result: | |
result = app.acquire_token_for_client(scopes=SCOPES) | |
if "access_token" in result: | |
return result['access_token'] | |
else: | |
st.error("Could not obtain access token.") | |
st.stop() | |
# Placeholder functions for MS Graph API interactions | |
def get_upcoming_events(): | |
# Implement API call to get upcoming events | |
return [] | |
def get_schedule(): | |
# Implement API call to get schedule | |
return [] | |
def add_event(event_details): | |
# Implement API call to add a new event | |
pass | |
def get_event_details(event_id): | |
# Implement API call to get event details | |
return {} | |
def filter_events(filter_criteria): | |
# Implement API call to filter events | |
return [] | |
# Sidebar navigation | |
st.sidebar.title("Navigation") | |
menu = st.sidebar.radio("Go to", [ | |
"1οΈβ£ Dashboard with Widgets", | |
"π Landing Page", | |
"π Upcoming Events", | |
"π Schedule", | |
"π Agenda", | |
"π Event Details", | |
"β Add Event", | |
"π Filter By" | |
]) | |
# Main content area | |
st.title("Simple Streamlit App with MS Graph API") | |
if menu == "1οΈβ£ Dashboard with Widgets": | |
st.header("1οΈβ£ Dashboard with Widgets") | |
st.write("Widgets will be displayed here.") | |
# Add your widgets here | |
elif menu == "π Landing Page": | |
st.header("π Landing Page") | |
st.write("Welcome to the app!") | |
# Add landing page content here | |
elif menu == "π Upcoming Events": | |
st.header("π Upcoming Events") | |
events = get_upcoming_events() | |
for event in events: | |
st.write(event) | |
# Display upcoming events | |
elif menu == "π Schedule": | |
st.header("π Schedule") | |
schedule = get_schedule() | |
st.write(schedule) | |
# Display schedule | |
elif menu == "π Agenda": | |
st.header("π Agenda") | |
# Display agenda | |
st.write("Your agenda for today.") | |
elif menu == "π Event Details": | |
st.header("π Event Details") | |
event_id = st.text_input("Enter Event ID") | |
if event_id: | |
event_details = get_event_details(event_id) | |
st.write(event_details) | |
# Display event details based on ID | |
elif menu == "β Add Event": | |
st.header("β Add Event") | |
event_title = st.text_input("Event Title") | |
event_date = st.date_input("Event Date") | |
event_time = st.time_input("Event Time") | |
if st.button("Add Event"): | |
event_details = { | |
"title": event_title, | |
"date": event_date, | |
"time": event_time | |
} | |
add_event(event_details) | |
st.success("Event added successfully!") | |
# Form to add new event | |
elif menu == "π Filter By": | |
st.header("π Filter Events") | |
filter_criteria = st.text_input("Enter filter criteria") | |
if filter_criteria: | |
filtered_events = filter_events(filter_criteria) | |
st.write(filtered_events) | |
# Filter events based on criteria | |
else: | |
st.write("Please select a menu option.") | |