Skip to content

Commit b8a62bd

Browse files
abhidnya13rayluo
authored andcommitted
Updating device code and username password samples to call MS Graph (#117)
* Updating device flow sample * Updating username password flow sample
1 parent 81bc034 commit b8a62bd

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

sample/device_flow_sample.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
{
55
"authority": "https://login.microsoftonline.com/common",
66
"client_id": "your_client_id",
7-
"scope": ["User.Read"]
7+
"scope": ["User.ReadBasic.All"],
8+
// You can find the other permission names from this document
9+
// https://docs.microsoft.com/en-us/graph/permissions-reference
10+
"endpoint": "https://graph.microsoft.com/v1.0/users"
11+
// You can find more Microsoft Graph API endpoints from Graph Explorer
12+
// https://developer.microsoft.com/en-us/graph/graph-explorer
813
}
914
1015
You can then run this sample with a JSON configuration file:
@@ -16,6 +21,7 @@
1621
import json
1722
import logging
1823

24+
import requests
1925
import msal
2026

2127

@@ -70,12 +76,12 @@
7076
# and then keep calling acquire_token_by_device_flow(flow) in your own customized loop.
7177

7278
if "access_token" in result:
73-
print(result["access_token"])
74-
print(result["token_type"])
75-
print(result["expires_in"]) # You don't normally need to care about this.
76-
# It will be good for at least 5 minutes.
79+
# Calling graph using the access token
80+
graph_data = requests.get( # Use token to call downstream service
81+
config["endpoint"],
82+
headers={'Authorization': 'Bearer ' + result['access_token']},).json()
83+
print("Graph API call result: %s" % json.dumps(graph_data, indent=2))
7784
else:
7885
print(result.get("error"))
7986
print(result.get("error_description"))
8087
print(result.get("correlation_id")) # You may need this when reporting a bug
81-

sample/username_password_sample.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
"authority": "https://login.microsoftonline.com/organizations",
66
"client_id": "your_client_id",
77
"username": "your_username@your_tenant.com",
8-
"scope": ["User.Read"],
9-
"password": "This is a sample only. You better NOT persist your password."
8+
"password": "This is a sample only. You better NOT persist your password.",
9+
"scope": ["User.ReadBasic.All"],
10+
// You can find the other permission names from this document
11+
// https://docs.microsoft.com/en-us/graph/permissions-reference
12+
"endpoint": "https://graph.microsoft.com/v1.0/users"
13+
// You can find more Microsoft Graph API endpoints from Graph Explorer
14+
// https://developer.microsoft.com/en-us/graph/graph-explorer
1015
}
1116
1217
You can then run this sample with a JSON configuration file:
@@ -18,6 +23,7 @@
1823
import json
1924
import logging
2025

26+
import requests
2127
import msal
2228

2329

@@ -51,10 +57,11 @@
5157
config["username"], config["password"], scopes=config["scope"])
5258

5359
if "access_token" in result:
54-
print(result["access_token"])
55-
print(result["token_type"])
56-
print(result["expires_in"]) # You don't normally need to care about this.
57-
# It will be good for at least 5 minutes.
60+
# Calling graph using the access token
61+
graph_data = requests.get( # Use token to call downstream service
62+
config["endpoint"],
63+
headers={'Authorization': 'Bearer ' + result['access_token']},).json()
64+
print("Graph API call result: %s" % json.dumps(graph_data, indent=2))
5865
else:
5966
print(result.get("error"))
6067
print(result.get("error_description"))

0 commit comments

Comments
 (0)