forked from microsoftgraph/msgraph-training-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (65 loc) · 2.21 KB
/
main.py
File metadata and controls
79 lines (65 loc) · 2.21 KB
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# <ProgramSnippet>
import asyncio
import configparser
from msgraph.generated.models.o_data_errors.o_data_error import ODataError
from graph import Graph
async def main():
print('Python Graph App-Only Tutorial\n')
# Load settings
config = configparser.ConfigParser()
config.read(['config.cfg', 'config.dev.cfg'])
azure_settings = config['azure']
graph: Graph = Graph(azure_settings)
choice = -1
while choice != 0:
print('Please choose one of the following options:')
print('0. Exit')
print('1. Display access token')
print('2. List users')
print('3. Make a Graph call')
try:
choice = int(input())
except ValueError:
choice = -1
try:
if choice == 0:
print('Goodbye...')
elif choice == 1:
await display_access_token(graph)
elif choice == 2:
await list_users(graph)
elif choice == 3:
await make_graph_call(graph)
else:
print('Invalid choice!\n')
except ODataError as odata_error:
print('Error:')
if odata_error.error:
print(odata_error.error.code, odata_error.error.message)
# </ProgramSnippet>
# <DisplayAccessTokenSnippet>
async def display_access_token(graph: Graph):
token = await graph.get_app_only_token()
print('App-only token:', token, '\n')
# </DisplayAccessTokenSnippet>
# <ListUsersSnippet>
async def list_users(graph: Graph):
users_page = await graph.get_users()
# Output each users's details
if users_page and users_page.value:
for user in users_page.value:
print('User:', user.display_name)
print(' ID:', user.id)
print(' Email:', user.mail)
# If @odata.nextLink is present
more_available = users_page.odata_next_link is not None
print('\nMore users available?', more_available, '\n')
# </ListUsersSnippet>
# <MakeGraphCallSnippet>
async def make_graph_call(graph: Graph):
await graph.make_graph_call()
# </MakeGraphCallSnippet>
# Run main
asyncio.run(main())