awacke1 commited on
Commit
5a29dc5
Β·
verified Β·
1 Parent(s): 18803a3

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()