|
| 1 | +""" |
| 2 | +The configuration file would look like this (sans those // comments): |
| 3 | +
|
| 4 | +{ |
| 5 | + "authority": "https://login.microsoftonline.com/organizations", |
| 6 | + "client_id": "your_client_id", |
| 7 | + "scope": ["https://graph.microsoft.com/.default"], |
| 8 | + // For more information about scopes for an app, refer: |
| 9 | + // https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow#second-case-access-token-request-with-a-certificate" |
| 10 | +
|
| 11 | + "thumbprint": "790E... The thumbprint generated by AAD when you upload your public cert", |
| 12 | + "private_key_file": "filename.pem" |
| 13 | + // For information about generating thumbprint and private key file, refer: |
| 14 | + // https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Client-Credentials#client-credentials-with-certificate |
| 15 | +} |
| 16 | +
|
| 17 | +You can then run this sample with a JSON configuration file: |
| 18 | +
|
| 19 | + python sample.py parameters.json |
| 20 | +""" |
| 21 | + |
| 22 | +import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] |
| 23 | +import json |
| 24 | +import logging |
| 25 | + |
| 26 | +import msal |
| 27 | + |
| 28 | + |
| 29 | +# Optional logging |
| 30 | +# logging.basicConfig(level=logging.DEBUG) |
| 31 | + |
| 32 | +config = json.load(open(sys.argv[1])) |
| 33 | + |
| 34 | +# Create a preferably long-lived app instance which maintains a token cache. |
| 35 | +app = msal.ConfidentialClientApplication( |
| 36 | + config["client_id"], authority=config["authority"], |
| 37 | + client_credential={"thumbprint": config["thumbprint"], "private_key": open(config['private_key_file']).read()}, |
| 38 | + # token_cache=... # Default cache is in memory only. |
| 39 | + # You can learn how to use SerializableTokenCache from |
| 40 | + # https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache |
| 41 | + ) |
| 42 | + |
| 43 | +# The pattern to acquire a token looks like this. |
| 44 | +result = None |
| 45 | + |
| 46 | +# Firstly, looks up a token from cache |
| 47 | +# Since we are looking for token for the current app, NOT for an end user, |
| 48 | +# notice we give account parameter as None. |
| 49 | +result = app.acquire_token_silent(config["scope"], account=None) |
| 50 | + |
| 51 | +if not result: |
| 52 | + logging.info("No suitable token exists in cache. Let's get a new one from AAD.") |
| 53 | + result = app.acquire_token_for_client(scopes=config["scope"]) |
| 54 | + |
| 55 | +if "access_token" in result: |
| 56 | + print(result["access_token"]) |
| 57 | + print(result["token_type"]) |
| 58 | + print(result["expires_in"]) # You don't normally need to care about this. |
| 59 | + # It will be good for at least 5 minutes. |
| 60 | +else: |
| 61 | + print(result.get("error")) |
| 62 | + print(result.get("error_description")) |
| 63 | + print(result.get("correlation_id")) # You may need this when reporting a bug |
| 64 | + |
0 commit comments