forked from microsoftgraph/msgraph-sdk-python-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_client_samples.py
103 lines (87 loc) · 2.92 KB
/
graph_client_samples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
#pylint: disable=undefined-variable
"""Demonstrates using the GraphClient to make HTTP Requests to Microsoft Graph"""
import json
from pprint import pprint
# This sample uses InteractiveBrowserCredential only for demonstration.
# Any azure-identity TokenCredential class will work the same.
from azure.identity import InteractiveBrowserCredential
from msgraph.core import APIVersion, GraphClient, NationalClouds
from requests import Session
scopes = ['user.read']
browser_credential = InteractiveBrowserCredential(client_id='YOUR_CLIENT_ID')
client = GraphClient(credential=browser_credential)
def get_sample():
"""Sample HTTP GET request using the GraphClient"""
result = client.get('/me/messages', scopes=['mail.read'])
pprint(result.json())
def post_sample():
"""Sample HTTP POST request using the GraphClient"""
body = {
'message': {
'subject': 'Python SDK Meet for lunch?',
'body': {
'contentType': 'Text',
'content': 'The new cafeteria is open.'
},
'toRecipients': [{
'emailAddress': {
'address': 'ENTER_RECEPIENT_EMAIL_ADDRESS'
}
}]
}
}
result = client \
.post('/me/sendMail',
data=json.dumps(body),
scopes=['mail.send'],
headers={'Content-Type': 'application/json'}
)
pprint(result.status_code)
def client_with_custom_session_sample():
"""Sample client with a custom Session object"""
session = Session()
my_client = GraphClient(credential=browser_credential, session=session)
result = my_client.get('/me')
pprint(result.json())
def client_with_custom_settings_sample():
"""
Sample client that makes requests against the beta api on a specified cloud endpoint
"""
my_client = GraphClient(
credential=browser_credential,
api_version=APIVersion.beta,
cloud=NationalClouds.Germany,
)
result = my_client.get(
'/users',
params={
'$select': 'displayName',
'$top': '10'
},
)
pprint(result.json())
def client_with_custom_middleware():
"""Sample client with a custom middleware chain"""
middleware = [
CustomAuthorizationHandler(),
MyCustomMiddleware(),
]
my_client = GraphClient(credential=browser_credential, middleware=middleware)
result = my_client.get(
'https://graph.microsoft.com/v1.0/users',
params={
'$select': 'displayName',
'$top': '10'
},
)
pprint(result.json())
if __name__ == '__main__':
post_sample()
get_sample()
client_with_custom_session_sample()
client_with_custom_settings_sample()
client_with_custom_middleware()