|
5 | 5 | "authority": "https://login.microsoftonline.com/organizations", |
6 | 6 | "client_id": "your_client_id came from https://learn.microsoft.com/entra/identity-platform/quickstart-register-app", |
7 | 7 | "username": "your_username@your_tenant.com", |
8 | | - "password": "This is a sample only. You better NOT persist your password.", |
9 | 8 | "scope": ["User.ReadBasic.All"], |
10 | 9 | // You can find the other permission names from this document |
11 | 10 | // https://docs.microsoft.com/en-us/graph/permissions-reference |
|
20 | 19 | """ |
21 | 20 |
|
22 | 21 | import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1] |
| 22 | +import getpass |
23 | 23 | import json |
24 | 24 | import logging |
25 | 25 | import time |
|
33 | 33 | # logging.getLogger("msal").setLevel(logging.INFO) # Optionally disable MSAL DEBUG logs |
34 | 34 |
|
35 | 35 | config = json.load(open(sys.argv[1])) |
| 36 | +config["password"] = getpass.getpass() |
36 | 37 |
|
37 | 38 | # If for whatever reason you plan to recreate same ClientApplication periodically, |
38 | 39 | # you shall create one global token cache and reuse it by each ClientApplication |
39 | 40 | global_token_cache = msal.TokenCache() # The TokenCache() is in-memory. |
40 | 41 | # See more options in https://msal-python.readthedocs.io/en/latest/#tokencache |
41 | 42 |
|
42 | 43 | # Create a preferably long-lived app instance, to avoid the overhead of app creation |
43 | | -global_app = msal.PublicClientApplication( |
44 | | - config["client_id"], authority=config["authority"], |
| 44 | +global_app = msal.ClientApplication( |
| 45 | + config["client_id"], |
45 | 46 | client_credential=config.get("client_secret"), |
| 47 | + authority=config["authority"], |
46 | 48 | token_cache=global_token_cache, # Let this app (re)use an existing token cache. |
47 | 49 | # If absent, ClientApplication will create its own empty token cache |
48 | 50 | ) |
@@ -73,8 +75,7 @@ def acquire_and_use_token(): |
73 | 75 | headers={'Authorization': 'Bearer ' + result['access_token']},).json() |
74 | 76 | print("Graph API call result: %s" % json.dumps(graph_data, indent=2)) |
75 | 77 | else: |
76 | | - print("Token acquisition failed") # Examine result["error_description"] etc. to diagnose error |
77 | | - print(result) |
| 78 | + print("Token acquisition failed", result) # Examine result["error_description"] etc. to diagnose error |
78 | 79 | if 65001 in result.get("error_codes", []): # Not mean to be coded programatically, but... |
79 | 80 | raise RuntimeError( |
80 | 81 | "AAD requires user consent for U/P flow to succeed. " |
|
0 commit comments