Azure MS Graph API Tutorial Help on Redirect URL

I’m writing a Azure MS Graph Demo on Huggingface, complete with a simple How-To tutorial to teach others.

I am stuck on the redirect url used to pass a webbrowser open redirect back to a huggingface URL of my space.

In MS graph API Authentication you set the Redirect URL to obtain a token which is then used to authenticate.

Can anyone help detemrine how to set a redirect URL so it works with Huggingface Spaces? If I debug locally and set url to localhost:8000 and use that I can get the URL with the token from localhost debug. How do I set it to redirect back to HF spaces?

MS Graph Auth Redirect URI:

Space:

Tutorial Docs:

Code:

import msal
import os
import streamlit as st
import webbrowser


from msal import PublicClientApplication

APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY ')
CLIENT_SECRET_KEY = os.getenv('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)
webbrowser.open(authorization_request_url, new=True)

authorization_code = '' #Get from url of webbrowser response - this is contingent on the URL set in MS Graph API Authentication section


access_token_id = access_token['access_token']
headers = {'Authorization': 'Bearer ' + access_token_id}

endpoint = base_url + 'me'
response = requests.get()

# 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

1 Like