Spaces:
Running
Running
File size: 5,499 Bytes
2bf52ad d620f19 dd82908 3d9aeea 3ef36dd 3012d35 d620f19 2bf52ad 4440844 5026787 c685413 9c7ea38 f50567e c685413 0269124 987fbf6 0269124 3d9aeea 0269124 2a845d5 3d9aeea 4be7001 56a055d ef9c007 3954aa4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
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.")
|