Skip to content

Instantly share code, notes, and snippets.

@agrazh
Last active September 30, 2025 08:30
Show Gist options
  • Save agrazh/107e7b71c96f44236c7126d97056e1d6 to your computer and use it in GitHub Desktop.
Save agrazh/107e7b71c96f44236c7126d97056e1d6 to your computer and use it in GitHub Desktop.
Azure passwordless authentication method
from msal import PublicClientApplication
# Azure CLI's well-known public client ID
client_id = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
tenant_id = "your-tenant-id" # Replace with your actual tenant ID
app = PublicClientApplication(
client_id=client_id,
authority=f"https://login.microsoftonline.com/{tenant_id}"
)
print("🔐 Starting authentication...")
print("Browser will open automatically\n")
# Minimal parameters - let MSAL handle everything
result = app.acquire_token_interactive(
scopes=["User.Read"]
)
if "access_token" in result:
print("✅ Authentication successful!")
print(f"Access Token: {result['access_token'][:50]}...")
# Test the token
import requests
headers = {"Authorization": f"Bearer {result['access_token']}"}
response = requests.get("https://graph.microsoft.com/v1.0/me", headers=headers)
if response.status_code == 200:
profile = response.json()
print(f"\n✅ Token works!")
print(f"Your name: {profile.get('displayName')}")
print(f"Your email: {profile.get('mail') or profile.get('userPrincipalName')}")
else:
print(f"❌ Authentication failed!")
print(f"Error: {result.get('error')}")
print(f"Description: {result.get('error_description')}")
print(f"\nFull response: {result}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment