|
2 | 2 | The configuration file would look like this (sans those // comments): |
3 | 3 |
|
4 | 4 | { |
5 | | - "authority": "https://login.microsoftonline.com/organizations", |
| 5 | + "authority": "https://login.microsoftonline.com/Enter_the_Tenant_Name_Here", |
6 | 6 | "client_id": "your_client_id", |
7 | 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" |
| 8 | + // Specific to Client Credentials Grant i.e. acquire_token_for_client(), |
| 9 | + // you don't specify, in the code, the individual scopes you want to access. |
| 10 | + // Instead, you statically declared them when registering your application. |
| 11 | + // Therefore the only possible scope is "resource/.default" |
| 12 | + // (here "https://graph.microsoft.com/.default") |
| 13 | + // which means "the static permissions defined in the application". |
10 | 14 |
|
11 | 15 | "thumbprint": "790E... The thumbprint generated by AAD when you upload your public cert", |
12 | | - "private_key_file": "filename.pem" |
| 16 | + "private_key_file": "filename.pem", |
13 | 17 | // For information about generating thumbprint and private key file, refer: |
14 | 18 | // https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Client-Credentials#client-credentials-with-certificate |
| 19 | +
|
| 20 | + "endpoint": "https://graph.microsoft.com/v1.0/users" |
| 21 | + // For this resource to work, you need to visit Application Permissions |
| 22 | + // page in portal, declare scope User.Read.All, which needs admin consent |
| 23 | + // https://github.com/Azure-Samples/ms-identity-python-daemon/blob/master/2-Call-MsGraph-WithCertificate/README.md |
15 | 24 | } |
16 | 25 |
|
17 | 26 | You can then run this sample with a JSON configuration file: |
|
23 | 32 | import json |
24 | 33 | import logging |
25 | 34 |
|
| 35 | +import requests |
26 | 36 | import msal |
27 | 37 |
|
28 | 38 |
|
29 | 39 | # Optional logging |
30 | | -# logging.basicConfig(level=logging.DEBUG) |
| 40 | +# logging.basicConfig(level=logging.DEBUG) # Enable DEBUG log for entire script |
| 41 | +# logging.getLogger("msal").setLevel(logging.INFO) # Optionally disable MSAL DEBUG logs |
31 | 42 |
|
32 | 43 | config = json.load(open(sys.argv[1])) |
33 | 44 |
|
|
53 | 64 | result = app.acquire_token_for_client(scopes=config["scope"]) |
54 | 65 |
|
55 | 66 | 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. |
| 67 | + # Calling graph using the access token |
| 68 | + graph_data = requests.get( # Use token to call downstream service |
| 69 | + config["endpoint"], |
| 70 | + headers={'Authorization': 'Bearer ' + result['access_token']},).json() |
| 71 | + print("Graph API call result: %s" % json.dumps(graph_data, indent=2)) |
60 | 72 | else: |
61 | 73 | print(result.get("error")) |
62 | 74 | print(result.get("error_description")) |
|
0 commit comments