Skip to content

Instantly share code, notes, and snippets.

@joaompinto
Created July 9, 2023 17:29
Show Gist options
  • Save joaompinto/73553adb2df060afd34f57c5af415cb6 to your computer and use it in GitHub Desktop.
Save joaompinto/73553adb2df060afd34f57c5af415cb6 to your computer and use it in GitHub Desktop.
# pip install adal
from msal import PublicClientApplication
from os import environ
app = PublicClientApplication(
environ['AZURE_CLIENT_ID'],
authority="https://login.microsoftonline.com/common")
result = None # It is just an initial value. Please follow instructions below.
# We now check the cache to see
# whether we already have some accounts that the end user already used to sign in before.
accounts = app.get_accounts()
if accounts:
# If so, you could then somehow display these accounts and let end user choose
print("Pick the account you want to use to proceed:")
for a in accounts:
print(a["username"])
# Assuming the end user chose this one
chosen = accounts[0]
# Now let's try to find a token in cache for this account
result = app.acquire_token_silent(["User.Read"], account=chosen)
if not result:
# So no suitable token exists in cache. Let's get a new one from Azure AD.
result = app.acquire_token_interactive(scopes=["User.Read"])
if "access_token" in result:
print(result["access_token"]) # Yay!
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id")) # You may need this when reporting a bug
# We get:
# invalid_request: The provided value for the input parameter 'redirect_uri' is not valid. The expected value is a URI which matches a redirect URI registered for this client application.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment