# Given the client ID and tenant ID for an app registered in Azure, # provide a access token and a refresh token. # If the caller is not already signed in to Azure, the caller's # web browser will prompt the caller to sign in first. # pip install msal from msal import PublicClientApplication import sys import os import stat # You can hard-code the registered app's client ID and tenant ID here, # or you can provide them as command-line arguments to this script. client_id = 'a8601a83-6185-498e-928d-91978c924162' tenant_id = 'ef2b4271-9238-4dcd-8c56-d3e915e37c6f' # Do not modify this variable. It represents the programmatic ID for # Azure Databricks along with the default scope of '/.default'. scopes = [ 'api://b7ae5060-667c-47b7-83f8-71283df2a2f6/session:scope:connect' ] # Check for too few or too many command-line arguments. if (len(sys.argv) > 1) and (len(sys.argv) != 3): print("Usage: get-tokens.py ") exit(1) # If the registered app's client ID and tenant ID are provided as # command-line variables, set them here. if len(sys.argv) > 1: client_id = sys.argv[1] tenant_id = sys.argv[2] app = PublicClientApplication( client_id = client_id, authority = "https://login.microsoftonline.com/" + tenant_id ) acquire_tokens_result = app.acquire_token_interactive( scopes = scopes ) if 'error' in acquire_tokens_result: print("Error: " + acquire_tokens_result['error']) print("Description: " + acquire_tokens_result['error_description']) else: access_token = acquire_tokens_result['access_token'] refresh_token = acquire_tokens_result['refresh_token'] print("Access token:\n") print(acquire_tokens_result['access_token']) print("\nRefresh token:\n") print(acquire_tokens_result['refresh_token']) # Write access token to file named 'token' with open('token', 'w') as token_file: token_file.write(access_token) os.chmod('token', stat.S_IRUSR | stat.S_IWUSR)