-
Notifications
You must be signed in to change notification settings - Fork 0
/
O365 Test
52 lines (38 loc) · 1.38 KB
/
O365 Test
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
import os
import sys
from o365.account import Account
from o365.middleware import AuthMiddleware
from o365.utils import FileSystemTokenBackend
# Set the path to the client secret file
client_secret_path = 'client_secret.json'
# Set the path to the token file
token_path = 'token.json'
# Set the path to the credentials file
credentials_path = 'credentials.json'
# Set the path to the log file
log_path = 'o365.log'
# Create a token backend object
token_backend = FileSystemTokenBackend(token_path, client_secret_path)
# Create a middleware object
middleware = AuthMiddleware(client_secret_path, token_backend=token_backend, scopes=['basic', 'contacts_all', 'contacts_read', 'mailbox_all', 'mailbox_read'])
# Create a new account object
account = Account(credentials_path, auth_provider=middleware)
# Log in to the Office 365 API
if account.authenticate(scopes=middleware.scopes):
print('Authenticated to the Office 365 API.')
else:
print('Failed to authenticate to the Office 365 API.')
sys.exit(1)
# Get the mailbox object
mailbox = account.mailbox()
# Get the inbox folder
inbox = mailbox.inbox_folder()
# Get the list of messages in the inbox
messages = inbox.get_messages()
# Print the subject and sender of each message
for message in messages:
print(f'Subject: {message.subject}')
print(f'Sender: {message.sender}')
print()
# Log out of the Office 365 API
account.logout()