File size: 4,353 Bytes
921344f
 
 
 
 
 
b513742
 
 
 
 
 
921344f
b513742
b822d8c
08c35e1
b513742
 
 
921344f
 
b513742
 
 
 
 
 
 
 
 
 
ec81794
b513742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec81794
b513742
 
 
ec81794
b513742
 
921344f
b513742
 
 
b19be65
 
 
b513742
c97ecbe
b513742
 
b19be65
c97ecbe
 
b513742
b19be65
a78ffeb
b19be65
b513742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
921344f
b513742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
921344f
 
b513742
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
import os
import streamlit as st
import requests
import msal
import secrets

# Configuration
APPLICATION_ID = os.getenv('APPLICATION_ID_KEY')
CLIENT_SECRET = os.getenv('CLIENT_SECRET_KEY')
AUTHORITY = 'https://login.microsoftonline.com/common'
REDIRECT_URI = 'https://huggingface.co/spaces/awacke1/MSGraphAPI'
SCOPES = ['User.Read', 'Calendars.ReadWrite', 'Mail.ReadWrite']

# MSAL setup
def get_msal_app():
    return msal.ConfidentialClientApplication(
        client_id=APPLICATION_ID,
        client_credential=CLIENT_SECRET,
        authority=AUTHORITY
    )

# Authentication functions
def generate_auth_url():
    msal_app = get_msal_app()
    state = secrets.token_urlsafe(32)
    st.session_state['auth_state'] = state
    return msal_app.get_authorization_request_url(
        scopes=SCOPES,
        redirect_uri=REDIRECT_URI,
        state=state
    )

def get_token_from_code(code):
    msal_app = get_msal_app()
    result = msal_app.acquire_token_by_authorization_code(
        code=code,
        scopes=SCOPES,
        redirect_uri=REDIRECT_URI
    )
    if 'access_token' in result:
        return result
    else:
        raise Exception(f"Error acquiring token: {result.get('error_description')}")

def get_token_from_cache():
    if 'token_cache' in st.session_state:
        msal_app = get_msal_app()
        accounts = msal_app.get_accounts()
        if accounts:
            result = msal_app.acquire_token_silent(SCOPES, account=accounts[0])
            if result:
                return result
    return None

# API call function
def make_api_call(endpoint, token):
    headers = {'Authorization': f'Bearer {token}'}
    response = requests.get(f'https://graph.microsoft.com/v1.0/{endpoint}', headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        st.error(f"API call failed: {response.status_code} - {response.text}")
        return None

# Main application
def main():
    st.title("๐Ÿฆ„ MS Graph API Integration")

    # Check for authentication
    token = get_token_from_cache()
    
    if 'code' in st.query_params:
        state = st.query_params.get('state')
        if state != st.session_state.get('auth_state'):
            st.error("Invalid state parameter. Please try logging in again.")
            st.session_state.clear()
            st.rerun()
        
        try:
            token = get_token_from_code(st.query_params['code'])
            st.session_state['token_cache'] = token
            st.success("Successfully authenticated!")
            st.rerun()
        except Exception as e:
            st.error(f"Authentication failed: {str(e)}")
            st.session_state.clear()

    if not token:
        auth_url = generate_auth_url()
        st.write("Please log in to continue:")
        st.markdown(f"[Login with Microsoft]({auth_url})")
        return

    # User is authenticated, show the main app
    st.sidebar.success("Authenticated successfully!")
    
    # Display user info
    user_info = make_api_call('me', token['access_token'])
    if user_info:
        st.sidebar.write(f"Welcome, {user_info.get('displayName', 'User')}!")

    # App functionality
    option = st.sidebar.selectbox(
        "Choose a function",
        ["View Emails", "View Calendar", "View OneDrive Files"]
    )

    if option == "View Emails":
        emails = make_api_call('me/messages?$top=10', token['access_token'])
        if emails:
            for email in emails['value']:
                st.write(f"Subject: {email['subject']}")
                st.write(f"From: {email['from']['emailAddress']['name']}")
                st.write("---")

    elif option == "View Calendar":
        events = make_api_call('me/events?$top=10', token['access_token'])
        if events:
            for event in events['value']:
                st.write(f"Event: {event['subject']}")
                st.write(f"Start: {event['start']['dateTime']}")
                st.write("---")

    elif option == "View OneDrive Files":
        files = make_api_call('me/drive/root/children', token['access_token'])
        if files:
            for file in files['value']:
                st.write(f"File: {file['name']}")
                st.write(f"Type: {file['file']['mimeType'] if 'file' in file else 'Folder'}")
                st.write("---")

if __name__ == "__main__":
    main()