Spaces:
Sleeping
Sleeping
Delete backup.IThinkWorkingNow-ImplementRest.app.py
Browse files
backup.IThinkWorkingNow-ImplementRest.app.py
DELETED
|
@@ -1,136 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import requests
|
| 4 |
-
import msal
|
| 5 |
-
import urllib.parse
|
| 6 |
-
import base64
|
| 7 |
-
import hashlib
|
| 8 |
-
import secrets
|
| 9 |
-
|
| 10 |
-
# Configuration
|
| 11 |
-
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY')
|
| 12 |
-
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY')
|
| 13 |
-
AUTHORITY_URL = 'https://login.microsoftonline.com/common'
|
| 14 |
-
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
|
| 15 |
-
|
| 16 |
-
# Define product to scope mapping
|
| 17 |
-
PRODUCT_SCOPES = {
|
| 18 |
-
"π§ Outlook": ['Mail.Read', 'Mail.Send', 'Calendars.ReadWrite'],
|
| 19 |
-
"π OneNote": ['Notes.Read', 'Notes.Create'],
|
| 20 |
-
"π Excel": ['Files.ReadWrite.All'],
|
| 21 |
-
"π Word": ['Files.ReadWrite.All'],
|
| 22 |
-
"ποΈ SharePoint": ['Sites.Read.All', 'Sites.ReadWrite.All'],
|
| 23 |
-
"π
Teams": ['Team.ReadBasic.All', 'Channel.ReadBasic.All'],
|
| 24 |
-
"π¬ Viva": ['Analytics.Read'],
|
| 25 |
-
"π Power Platform": ['Flow.Read.All'],
|
| 26 |
-
"π§ Copilot": ['Cognitive.Read'],
|
| 27 |
-
"ποΈ OneDrive": ['Files.ReadWrite.All'],
|
| 28 |
-
"π‘ PowerPoint": ['Files.ReadWrite.All'],
|
| 29 |
-
"π Microsoft Bookings": ['Bookings.Read.All', 'Bookings.ReadWrite.All'],
|
| 30 |
-
"π Loop": ['Files.ReadWrite.All'],
|
| 31 |
-
"π£οΈ Translator": ['Translation.Read'],
|
| 32 |
-
"π To Do & Planner": ['Tasks.ReadWrite'],
|
| 33 |
-
"π Azure OpenAI Service": ['AzureAIServices.ReadWrite.All']
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
# Base scopes (non-reserved)
|
| 37 |
-
BASE_SCOPES = ['User.Read']
|
| 38 |
-
|
| 39 |
-
def get_msal_app():
|
| 40 |
-
return msal.ConfidentialClientApplication(
|
| 41 |
-
client_id=APPLICATION_ID_KEY,
|
| 42 |
-
client_credential=CLIENT_SECRET_KEY,
|
| 43 |
-
authority=AUTHORITY_URL
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
def get_access_token(code):
|
| 47 |
-
client_instance = get_msal_app()
|
| 48 |
-
|
| 49 |
-
try:
|
| 50 |
-
result = client_instance.acquire_token_by_authorization_code(
|
| 51 |
-
code=code,
|
| 52 |
-
scopes=st.session_state.get('request_scopes', BASE_SCOPES),
|
| 53 |
-
redirect_uri=REDIRECT_URI
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
if 'access_token' in result:
|
| 57 |
-
return result['access_token']
|
| 58 |
-
else:
|
| 59 |
-
error_description = result.get('error_description', 'No error description provided')
|
| 60 |
-
raise Exception(f"Error acquiring token: {error_description}")
|
| 61 |
-
except Exception as e:
|
| 62 |
-
st.error(f"Exception in get_access_token: {str(e)}")
|
| 63 |
-
raise
|
| 64 |
-
|
| 65 |
-
def main():
|
| 66 |
-
st.title("π¦ MS Graph API with AI & Cloud Integration for M365")
|
| 67 |
-
|
| 68 |
-
st.sidebar.title("π M365 Products")
|
| 69 |
-
st.sidebar.write("Select products to integrate:")
|
| 70 |
-
|
| 71 |
-
selected_products = {}
|
| 72 |
-
for product in PRODUCT_SCOPES.keys():
|
| 73 |
-
selected = st.sidebar.checkbox(product)
|
| 74 |
-
if selected:
|
| 75 |
-
selected_products[product] = True
|
| 76 |
-
|
| 77 |
-
# Dynamically build scopes based on selected products
|
| 78 |
-
request_scopes = BASE_SCOPES.copy()
|
| 79 |
-
for product in selected_products:
|
| 80 |
-
request_scopes.extend(PRODUCT_SCOPES[product])
|
| 81 |
-
request_scopes = list(set(request_scopes)) # Remove duplicates
|
| 82 |
-
|
| 83 |
-
# Store request scopes in session state
|
| 84 |
-
st.session_state['request_scopes'] = request_scopes
|
| 85 |
-
|
| 86 |
-
if 'access_token' not in st.session_state:
|
| 87 |
-
client_instance = get_msal_app()
|
| 88 |
-
auth_url = client_instance.get_authorization_request_url(
|
| 89 |
-
scopes=request_scopes,
|
| 90 |
-
redirect_uri=REDIRECT_URI
|
| 91 |
-
)
|
| 92 |
-
st.write('π Please [click here]({}) to log in and authorize the app.'.format(auth_url))
|
| 93 |
-
|
| 94 |
-
query_params = st.query_params
|
| 95 |
-
if 'code' in query_params:
|
| 96 |
-
code = query_params.get('code')
|
| 97 |
-
st.write('π Authorization Code Obtained:', code[:10] + '...')
|
| 98 |
-
|
| 99 |
-
try:
|
| 100 |
-
access_token = get_access_token(code)
|
| 101 |
-
st.session_state['access_token'] = access_token
|
| 102 |
-
st.success("Access token acquired successfully!")
|
| 103 |
-
st.rerun()
|
| 104 |
-
except Exception as e:
|
| 105 |
-
st.error(f"Error acquiring access token: {str(e)}")
|
| 106 |
-
st.stop()
|
| 107 |
-
else:
|
| 108 |
-
access_token = st.session_state['access_token']
|
| 109 |
-
|
| 110 |
-
user_info = get_user_info(access_token)
|
| 111 |
-
if user_info:
|
| 112 |
-
st.sidebar.write(f"π Hello, {user_info.get('displayName', 'User')}!")
|
| 113 |
-
|
| 114 |
-
if selected_products:
|
| 115 |
-
st.header("π§© M365 Product Integrations")
|
| 116 |
-
for product in selected_products:
|
| 117 |
-
st.subheader(f"{product}")
|
| 118 |
-
handle_product_integration(access_token, product)
|
| 119 |
-
else:
|
| 120 |
-
st.write("No products selected. Please select products from the sidebar.")
|
| 121 |
-
|
| 122 |
-
def get_user_info(access_token):
|
| 123 |
-
headers = {'Authorization': f'Bearer {access_token}'}
|
| 124 |
-
response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
|
| 125 |
-
if response.status_code == 200:
|
| 126 |
-
return response.json()
|
| 127 |
-
else:
|
| 128 |
-
st.error('Failed to fetch user info.')
|
| 129 |
-
return None
|
| 130 |
-
|
| 131 |
-
def handle_product_integration(access_token, product):
|
| 132 |
-
st.write(f"Integrating {product}...")
|
| 133 |
-
# Implement product-specific API calls here
|
| 134 |
-
|
| 135 |
-
if __name__ == "__main__":
|
| 136 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|