Skip to content

Commit e71c791

Browse files
authored
Updating samples to call MS graph (#112)
1 parent d226142 commit e71c791

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

sample/confidential_client_certificate_sample.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
The configuration file would look like this (sans those // comments):
33
44
{
5-
"authority": "https://login.microsoftonline.com/organizations",
5+
"authority": "https://login.microsoftonline.com/Enter_the_Tenant_Name_Here",
66
"client_id": "your_client_id",
77
"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".
1014
1115
"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",
1317
// For information about generating thumbprint and private key file, refer:
1418
// 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
1524
}
1625
1726
You can then run this sample with a JSON configuration file:
@@ -23,6 +32,7 @@
2332
import json
2433
import logging
2534

35+
import requests
2636
import msal
2737

2838

@@ -53,10 +63,11 @@
5363
result = app.acquire_token_for_client(scopes=config["scope"])
5464

5565
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.
66+
# Calling graph using the access token
67+
graph_data = requests.get( # Use token to call downstream service
68+
config["endpoint"],
69+
headers={'Authorization': 'Bearer ' + result['access_token']},).json()
70+
print("Graph API call result: " + str(graph_data))
6071
else:
6172
print(result.get("error"))
6273
print(result.get("error_description"))

sample/confidential_client_secret_sample.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22
The configuration file would look like this (sans those // comments):
33
44
{
5-
"authority": "https://login.microsoftonline.com/organizations",
5+
"authority": "https://login.microsoftonline.com/Enter_the_Tenant_Name_Here",
66
"client_id": "your_client_id",
77
"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-
"secret": "The secret generated by AAD during your confidential app registration"
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".
14+
15+
"secret": "The secret generated by AAD during your confidential app registration",
1216
// For information about generating client secret, refer:
1317
// https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Client-Credentials#registering-client-secrets-using-the-application-registration-portal
1418
19+
"endpoint": "https://graph.microsoft.com/v1.0/users"
20+
// For this resource to work, you need to visit Application Permissions
21+
// page in portal, declare scope User.Read.All, which needs admin consent
22+
// https://github.com/Azure-Samples/ms-identity-python-daemon/blob/master/1-Call-MsGraph-WithSecret/README.md
1523
}
1624
1725
You can then run this sample with a JSON configuration file:
@@ -23,6 +31,7 @@
2331
import json
2432
import logging
2533

34+
import requests
2635
import msal
2736

2837

@@ -53,10 +62,12 @@
5362
result = app.acquire_token_for_client(scopes=config["scope"])
5463

5564
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.
65+
# Calling graph using the access token
66+
graph_data = requests.get( # Use token to call downstream service
67+
config["endpoint"],
68+
headers={'Authorization': 'Bearer ' + result['access_token']},).json()
69+
print("Graph API call result: " + str(graph_data))
70+
6071
else:
6172
print(result.get("error"))
6273
print(result.get("error_description"))

0 commit comments

Comments
 (0)