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
112 lines (95 loc) · 3.36 KB
/
main.py
File metadata and controls
112 lines (95 loc) · 3.36 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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 Tutorial\n')
# Load settings
config = configparser.ConfigParser()
config.read(['config.cfg', 'config.dev.cfg'])
azure_settings = config['azure']
graph: Graph = Graph(azure_settings)
await greet_user(graph)
choice = -1
while choice != 0:
print('Please choose one of the following options:')
print('0. Exit')
print('1. Display access token')
print('2. List my inbox')
print('3. Send mail')
print('4. 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_inbox(graph)
elif choice == 3:
await send_mail(graph)
elif choice == 4:
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>
# <GreetUserSnippet>
async def greet_user(graph: Graph):
user = await graph.get_user()
if user:
print('Hello,', user.display_name)
# For Work/school accounts, email is in mail property
# Personal accounts, email is in userPrincipalName
print('Email:', user.mail or user.user_principal_name, '\n')
# </GreetUserSnippet>
# <DisplayAccessTokenSnippet>
async def display_access_token(graph: Graph):
token = await graph.get_user_token()
print('User token:', token, '\n')
# </DisplayAccessTokenSnippet>
# <ListInboxSnippet>
async def list_inbox(graph: Graph):
message_page = await graph.get_inbox()
if message_page and message_page.value:
# Output each message's details
for message in message_page.value:
print('Message:', message.subject)
if (
message.from_ and
message.from_.email_address
):
print(' From:', message.from_.email_address.name or 'NONE')
else:
print(' From: NONE')
print(' Status:', 'Read' if message.is_read else 'Unread')
print(' Received:', message.received_date_time)
# If @odata.nextLink is present
more_available = message_page.odata_next_link is not None
print('\nMore messages available?', more_available, '\n')
# </ListInboxSnippet>
# <SendMailSnippet>
async def send_mail(graph: Graph):
# Send mail to the signed-in user
# Get the user for their email address
user = await graph.get_user()
if user:
user_email = user.mail or user.user_principal_name
await graph.send_mail('Testing Microsoft Graph', 'Hello world!', user_email or '')
print('Mail sent.\n')
# </SendMailSnippet>
# <MakeGraphCallSnippet>
async def make_graph_call(graph: Graph):
await graph.make_graph_call()
# </MakeGraphCallSnippet>
# Run main
asyncio.run(main())