Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,140 +2,134 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import msal
|
| 5 |
-
import base64
|
| 6 |
-
import hashlib
|
| 7 |
import secrets
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
def
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
try:
|
| 41 |
-
# Attempt to acquire token, use PKCE code_verifier if provided
|
| 42 |
-
result = client_instance.acquire_token_by_authorization_code(
|
| 43 |
-
code=code,
|
| 44 |
-
scopes=SCOPES,
|
| 45 |
-
redirect_uri=REDIRECT_URI,
|
| 46 |
-
code_verifier=code_verifier # Include only if PKCE is enabled
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
if 'access_token' in result:
|
| 50 |
-
return result['access_token']
|
| 51 |
-
else:
|
| 52 |
-
error_description = result.get('error_description', 'No error description provided')
|
| 53 |
-
raise Exception(f"Error acquiring token: {error_description}")
|
| 54 |
-
except Exception as e:
|
| 55 |
-
st.error(f"Exception in get_access_token: {str(e)}")
|
| 56 |
-
raise
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
def
|
| 60 |
-
|
| 61 |
-
query_params = st.experimental_get_query_params()
|
| 62 |
-
st.write("Debug: All query parameters:", query_params)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
st.error(f"Authentication Error: {error}")
|
| 68 |
-
st.error(f"Error Description: {error_description}")
|
| 69 |
-
st.stop()
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
st.error(f"Error acquiring access token: {str(e)}")
|
| 92 |
-
st.stop()
|
| 93 |
-
else:
|
| 94 |
-
st.warning("No authorization code found in the query parameters.")
|
| 95 |
-
except Exception as e:
|
| 96 |
-
st.error(f"Error processing query parameters: {str(e)}")
|
| 97 |
-
st.stop()
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
code_challenge=code_challenge,
|
| 118 |
-
code_challenge_method="S256"
|
| 119 |
-
)
|
| 120 |
-
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 121 |
-
st.stop()
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
|
| 139 |
-
# π Run the main function
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
main()
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import msal
|
|
|
|
|
|
|
| 5 |
import secrets
|
| 6 |
+
import time
|
| 7 |
+
from urllib.parse import urlencode
|
| 8 |
|
| 9 |
+
# Configuration
|
| 10 |
+
APPLICATION_ID = os.getenv('APPLICATION_ID_KEY')
|
| 11 |
+
CLIENT_SECRET = os.getenv('CLIENT_SECRET_KEY')
|
| 12 |
+
AUTHORITY = 'https://login.microsoftonline.com/common'
|
| 13 |
+
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 14 |
+
SCOPES = ['User.Read', 'Calendars.ReadWrite', 'Mail.ReadWrite']
|
| 15 |
|
| 16 |
+
# MSAL setup
|
| 17 |
+
def get_msal_app():
|
| 18 |
+
return msal.ConfidentialClientApplication(
|
| 19 |
+
client_id=APPLICATION_ID,
|
| 20 |
+
client_credential=CLIENT_SECRET,
|
| 21 |
+
authority=AUTHORITY
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Authentication functions
|
| 25 |
+
def generate_auth_url():
|
| 26 |
+
msal_app = get_msal_app()
|
| 27 |
+
state = secrets.token_urlsafe(32)
|
| 28 |
+
auth_url = msal_app.get_authorization_request_url(
|
| 29 |
+
scopes=SCOPES,
|
| 30 |
+
redirect_uri=REDIRECT_URI,
|
| 31 |
+
state=state
|
| 32 |
+
)
|
| 33 |
+
# Store the state in query params
|
| 34 |
+
new_query_params = st.query_params.to_dict()
|
| 35 |
+
new_query_params['auth_state'] = state
|
| 36 |
+
return f"{auth_url}&{urlencode(new_query_params)}"
|
| 37 |
|
| 38 |
+
def get_token_from_code(code):
|
| 39 |
+
msal_app = get_msal_app()
|
| 40 |
+
result = msal_app.acquire_token_by_authorization_code(
|
| 41 |
+
code=code,
|
| 42 |
+
scopes=SCOPES,
|
| 43 |
+
redirect_uri=REDIRECT_URI
|
| 44 |
)
|
| 45 |
+
if 'access_token' in result:
|
| 46 |
+
return result
|
| 47 |
+
else:
|
| 48 |
+
raise Exception(f"Error acquiring token: {result.get('error_description')}")
|
| 49 |
|
| 50 |
+
# API call function
|
| 51 |
+
def make_api_call(endpoint, token):
|
| 52 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 53 |
+
response = requests.get(f'https://graph.microsoft.com/v1.0/{endpoint}', headers=headers)
|
| 54 |
+
if response.status_code == 200:
|
| 55 |
+
return response.json()
|
| 56 |
+
else:
|
| 57 |
+
st.error(f"API call failed: {response.status_code} - {response.text}")
|
| 58 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
# Main application
|
| 61 |
+
def main():
|
| 62 |
+
st.title("π¦ MS Graph API Integration")
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Debug information
|
| 65 |
+
st.sidebar.write("Debug Info:")
|
| 66 |
+
st.sidebar.write(f"Query Params: {st.query_params.to_dict()}")
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
if 'code' in st.query_params and 'state' in st.query_params:
|
| 69 |
+
received_state = st.query_params['state']
|
| 70 |
+
expected_state = st.query_params.get('auth_state')
|
| 71 |
+
|
| 72 |
+
if received_state != expected_state:
|
| 73 |
+
st.error(f"Invalid state parameter. Expected {expected_state}, got {received_state}")
|
| 74 |
+
st.error("Please try logging in again.")
|
| 75 |
+
st.query_params.clear()
|
| 76 |
+
st.rerun()
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
token = get_token_from_code(st.query_params['code'])
|
| 80 |
+
st.session_state['token'] = token
|
| 81 |
+
st.query_params.clear()
|
| 82 |
+
st.success("Successfully authenticated!")
|
| 83 |
+
st.rerun()
|
| 84 |
+
except Exception as e:
|
| 85 |
+
st.error(f"Authentication failed: {str(e)}")
|
| 86 |
+
st.query_params.clear()
|
| 87 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
if 'token' not in st.session_state:
|
| 90 |
+
auth_url = generate_auth_url()
|
| 91 |
+
st.write("Please log in to continue:")
|
| 92 |
+
st.markdown(f"[Login with Microsoft]({auth_url})")
|
| 93 |
+
return
|
| 94 |
|
| 95 |
+
# User is authenticated, show the main app
|
| 96 |
+
token = st.session_state['token']
|
| 97 |
+
st.sidebar.success("Authenticated successfully!")
|
| 98 |
+
|
| 99 |
+
# Display user info
|
| 100 |
+
user_info = make_api_call('me', token['access_token'])
|
| 101 |
+
if user_info:
|
| 102 |
+
st.sidebar.write(f"Welcome, {user_info.get('displayName', 'User')}!")
|
| 103 |
|
| 104 |
+
# App functionality
|
| 105 |
+
option = st.sidebar.selectbox(
|
| 106 |
+
"Choose a function",
|
| 107 |
+
["View Emails", "View Calendar", "View OneDrive Files"]
|
| 108 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
if option == "View Emails":
|
| 111 |
+
emails = make_api_call('me/messages?$top=10', token['access_token'])
|
| 112 |
+
if emails:
|
| 113 |
+
for email in emails['value']:
|
| 114 |
+
st.write(f"Subject: {email['subject']}")
|
| 115 |
+
st.write(f"From: {email['from']['emailAddress']['name']}")
|
| 116 |
+
st.write("---")
|
| 117 |
|
| 118 |
+
elif option == "View Calendar":
|
| 119 |
+
events = make_api_call('me/events?$top=10', token['access_token'])
|
| 120 |
+
if events:
|
| 121 |
+
for event in events['value']:
|
| 122 |
+
st.write(f"Event: {event['subject']}")
|
| 123 |
+
st.write(f"Start: {event['start']['dateTime']}")
|
| 124 |
+
st.write("---")
|
| 125 |
|
| 126 |
+
elif option == "View OneDrive Files":
|
| 127 |
+
files = make_api_call('me/drive/root/children', token['access_token'])
|
| 128 |
+
if files:
|
| 129 |
+
for file in files['value']:
|
| 130 |
+
st.write(f"File: {file['name']}")
|
| 131 |
+
st.write(f"Type: {file['file']['mimeType'] if 'file' in file else 'Folder'}")
|
| 132 |
+
st.write("---")
|
| 133 |
|
|
|
|
| 134 |
if __name__ == "__main__":
|
| 135 |
+
main()
|