Created
August 18, 2026 17:59
-
-
Save sbealer/c19011a5017337996685e5eae8c77733 to your computer and use it in GitHub Desktop.
Personal MS365 token cache generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import msal | |
| # ------------------------------------------------------------------ | |
| # Azure / Entra application configuration | |
| # ------------------------------------------------------------------ | |
| CLIENT_ID = "your client id" #https://entra.microsoft.com/#view/Microsoft_AAD_RegisteredApp | |
| TENANT_ID = "your tenant id" | |
| login_hint = "your email" | |
| AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}" | |
| # These match the permissions present in your existing cache. | |
| SCOPES = [ | |
| "Files.ReadWrite", | |
| "MyFiles.Read", | |
| "MyFiles.Write", | |
| "User.Read", | |
| "email", | |
| ] | |
| CACHE_FILE = "token_cache.json" | |
| # ------------------------------------------------------------------ | |
| # Create a new MSAL token cache | |
| # ------------------------------------------------------------------ | |
| cache = msal.SerializableTokenCache() | |
| app = msal.PublicClientApplication( | |
| client_id=CLIENT_ID, | |
| authority=AUTHORITY, | |
| token_cache=cache | |
| ) | |
| # ------------------------------------------------------------------ | |
| # Authenticate interactively | |
| # | |
| # This will open your browser and have you log into Microsoft 365. | |
| # Complete MFA if Microsoft requests it. | |
| # ------------------------------------------------------------------ | |
| result = app.acquire_token_interactive( | |
| scopes=SCOPES, | |
| login_hint=login_hint | |
| # ------------------------------------------------------------------ | |
| # Validate authentication | |
| # ------------------------------------------------------------------ | |
| if "access_token" not in result: | |
| print("Authentication failed.") | |
| print() | |
| print("Error:", result.get("error")) | |
| print("Description:", result.get("error_description")) | |
| raise RuntimeError("Unable to acquire Microsoft 365 access token.") | |
| # ------------------------------------------------------------------ | |
| # Save the entire MSAL cache | |
| # | |
| # This contains: | |
| # - Access token | |
| # - Refresh token | |
| # - ID token | |
| # - Account information | |
| # | |
| # Your application can then use acquire_token_silent() against this | |
| # cache without requiring an interactive login every time. | |
| # ------------------------------------------------------------------ | |
| with open(CACHE_FILE, "w", encoding="utf-8") as f: | |
| f.write(cache.serialize()) | |
| # ------------------------------------------------------------------ | |
| # Display result | |
| # ------------------------------------------------------------------ | |
| claims = result.get("id_token_claims", {}) | |
| print() | |
| print("Authentication successful.") | |
| print("Account:", claims.get("preferred_username")) | |
| print("Name:", claims.get("name")) | |
| print("Cache written to:", CACHE_FILE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment