Skip to content

Commit 92d094a

Browse files
authored
Merge pull request #7 from AzureAD/samples
Provided first 3 samples to this repo
2 parents 6efccb0 + 1f9d674 commit 92d094a

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

sample/client_credential_sample.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
The configuration file would look like this:
3+
4+
{
5+
"authority": "https://login.microsoftonline.com/organizations",
6+
"client_id": "your_client_id",
7+
"secret": "This is a sample only. You better NOT persist your password."
8+
"scope": ["https://graph.microsoft.com/.default"]
9+
}
10+
11+
You can then run this sample with a JSON configuration file:
12+
13+
python sample.py parameters.json
14+
"""
15+
16+
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1]
17+
import json
18+
import logging
19+
20+
import msal
21+
22+
23+
# Optional logging
24+
# logging.basicConfig(level=logging.DEBUG)
25+
26+
config = json.load(open(sys.argv[1]))
27+
28+
# Create a preferably long-lived app instance which maintains a token cache.
29+
app = msal.ConfidentialClientApplication(
30+
config["client_id"], authority=config["authority"],
31+
client_credential=config["secret"],
32+
# token_cache=... # Default cache is in memory only.
33+
# See SerializableTokenCache for more details.
34+
)
35+
36+
# The pattern to acquire a token looks like this.
37+
result = None
38+
39+
# Firstly, looks up a token from cache
40+
# Since we are looking for token for the current app, NOT for an end user,
41+
# notice we give account parameter as None.
42+
result = app.acquire_token_silent(config["scope"], account=None)
43+
44+
if not result:
45+
# So no suitable token exists in cache. Let's get a new one from AAD.
46+
result = app.acquire_token_for_client(scopes=config["scope"])
47+
48+
if "access_token" in result:
49+
print(result["access_token"])
50+
print(result["token_type"])
51+
print(result["expires_in"]) # You don't normally need to care about this.
52+
# It will be good for at least 5 minutes.
53+
else:
54+
print(result.get("error"))
55+
print(result.get("error_description"))
56+
print(result.get("correlation_id")) # You may need this when reporting a bug
57+

sample/device_flow_sample.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
The configuration file would look like this:
3+
4+
{
5+
"authority": "https://login.microsoftonline.com/organizations",
6+
"client_id": "your_client_id",
7+
"scope": ["user.read"]
8+
}
9+
10+
You can then run this sample with a JSON configuration file:
11+
12+
python sample.py parameters.json
13+
"""
14+
15+
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1]
16+
import json
17+
import logging
18+
19+
import msal
20+
21+
22+
# Optional logging
23+
# logging.basicConfig(level=logging.DEBUG)
24+
25+
config = json.load(open(sys.argv[1]))
26+
27+
# Create a preferably long-lived app instance which maintains a token cache.
28+
app = msal.PublicClientApplication(
29+
config["client_id"], authority=config["authority"],
30+
# token_cache=... # Default cache is in memory only.
31+
# See SerializableTokenCache for more details.
32+
)
33+
34+
# The pattern to acquire a token looks like this.
35+
result = None
36+
37+
# Note: If your device-flow app does not have any interactive ability, you can
38+
# completely skip the following cache part. But here we demonstrate it anyway.
39+
# We now check the cache to see if we have some end users signed in before.
40+
accounts = app.get_accounts()
41+
if accounts:
42+
# If so, you could then somehow display these accounts and let end user choose
43+
print("Pick the account you want to use to proceed:")
44+
for a in accounts:
45+
print(a["username"])
46+
# Assuming the end user chose this one
47+
chosen = accounts[0]
48+
# Now let's try to find a token in cache for this account
49+
result = app.acquire_token_silent(config["scope"], account=chosen)
50+
51+
if not result:
52+
# So no suitable token exists in cache. Let's get a new one from AAD.
53+
flow = app.initiate_device_flow(scopes=config["scope"])
54+
print(flow["message"])
55+
# Ideally you should wait here, in order to save some unnecessary polling
56+
# input("Press Enter after you successfully login from another device...")
57+
result = app.acquire_token_by_device_flow(flow) # By default it will block
58+
59+
if "access_token" in result:
60+
print(result["access_token"])
61+
print(result["token_type"])
62+
print(result["expires_in"]) # You don't normally need to care about this.
63+
# It will be good for at least 5 minutes.
64+
else:
65+
print(result.get("error"))
66+
print(result.get("error_description"))
67+
print(result.get("correlation_id")) # You may need this when reporting a bug
68+

sample/username_password_sample.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
The configuration file would look like this:
3+
4+
{
5+
"authority": "https://login.microsoftonline.com/organizations",
6+
"client_id": "your_client_id",
7+
"username": "your_username@your_tenant.com",
8+
"scope": ["user.read"],
9+
"password": "This is a sample only. You better NOT persist your password."
10+
}
11+
12+
You can then run this sample with a JSON configuration file:
13+
14+
python sample.py parameters.json
15+
"""
16+
17+
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1]
18+
import json
19+
import logging
20+
21+
import msal
22+
23+
24+
# Optional logging
25+
# logging.basicConfig(level=logging.DEBUG)
26+
27+
config = json.load(open(sys.argv[1]))
28+
29+
# Create a preferably long-lived app instance which maintains a token cache.
30+
app = msal.PublicClientApplication(
31+
config["client_id"], authority=config["authority"],
32+
# token_cache=... # Default cache is in memory only.
33+
# See SerializableTokenCache for more details.
34+
)
35+
36+
# The pattern to acquire a token looks like this.
37+
result = None
38+
39+
# Firstly, check the cache to see if this end user has signed in before
40+
accounts = app.get_accounts(username=config["username"])
41+
if accounts:
42+
# It means the account(s) exists in cache, probably with token too. Let's try.
43+
result = app.acquire_token_silent(config["scope"], account=accounts[0])
44+
45+
if not result:
46+
# So no suitable token exists in cache. Let's get a new one from AAD.
47+
result = app.acquire_token_by_username_password(
48+
config["username"], config["password"], scopes=config["scope"])
49+
50+
if "access_token" in result:
51+
print(result["access_token"])
52+
print(result["token_type"])
53+
print(result["expires_in"]) # You don't normally need to care about this.
54+
# It will be good for at least 5 minutes.
55+
else:
56+
print(result.get("error"))
57+
print(result.get("error_description"))
58+
print(result.get("correlation_id")) # You may need this when reporting a bug
59+

0 commit comments

Comments
 (0)