forked from microsoftgraph/msgraph-training-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
105 lines (90 loc) · 3.99 KB
/
graph.py
File metadata and controls
105 lines (90 loc) · 3.99 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# <UserAuthConfigSnippet>
from configparser import SectionProxy
from azure.identity import DeviceCodeCredential
from kiota_authentication_azure.azure_identity_authentication_provider import (
AzureIdentityAuthenticationProvider)
from msgraph import GraphRequestAdapter, GraphServiceClient
from msgraph.generated.me.me_request_builder import MeRequestBuilder
from msgraph.generated.me.mail_folders.item.messages.messages_request_builder import (
MessagesRequestBuilder)
from msgraph.generated.me.send_mail.send_mail_post_request_body import SendMailPostRequestBody
from msgraph.generated.models.message import Message
from msgraph.generated.models.item_body import ItemBody
from msgraph.generated.models.body_type import BodyType
from msgraph.generated.models.recipient import Recipient
from msgraph.generated.models.email_address import EmailAddress
class Graph:
settings: SectionProxy
device_code_credential: DeviceCodeCredential
adapter: GraphRequestAdapter
user_client: GraphServiceClient
def __init__(self, config: SectionProxy):
self.settings = config
client_id = self.settings['clientId']
tenant_id = self.settings['tenantId']
graph_scopes = self.settings['graphUserScopes'].split(' ')
self.device_code_credential = DeviceCodeCredential(client_id, tenant_id = tenant_id)
auth_provider = AzureIdentityAuthenticationProvider(
self.device_code_credential,
scopes=graph_scopes)
self.adapter = GraphRequestAdapter(auth_provider)
self.user_client = GraphServiceClient(self.adapter)
# </UserAuthConfigSnippet>
# <GetUserTokenSnippet>
async def get_user_token(self):
graph_scopes = self.settings['graphUserScopes']
access_token = self.device_code_credential.get_token(graph_scopes)
return access_token.token
# </GetUserTokenSnippet>
# <GetUserSnippet>
async def get_user(self):
# Only request specific properties using $select
query_params = MeRequestBuilder.MeRequestBuilderGetQueryParameters(
select=['displayName', 'mail', 'userPrincipalName']
)
request_config = MeRequestBuilder.MeRequestBuilderGetRequestConfiguration(
query_parameters=query_params
)
user = await self.user_client.me.get(request_configuration=request_config)
return user
# </GetUserSnippet>
# <GetInboxSnippet>
async def get_inbox(self):
query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
# Only request specific properties
select=['from', 'isRead', 'receivedDateTime', 'subject'],
# Get at most 25 results
top=25,
# Sort by received time, newest first
orderby=['receivedDateTime DESC']
)
request_config = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
query_parameters= query_params
)
messages = await self.user_client.me.mail_folders_by_id('inbox').messages.get(
request_configuration=request_config)
return messages
# </GetInboxSnippet>
# <SendMailSnippet>
async def send_mail(self, subject: str, body: str, recipient: str):
message = Message()
message.subject = subject
message.body = ItemBody()
message.body.content_type = BodyType.Text
message.body.content = body
to_recipient = Recipient()
to_recipient.email_address = EmailAddress()
to_recipient.email_address.address = recipient
message.to_recipients = []
message.to_recipients.append(to_recipient)
request_body = SendMailPostRequestBody()
request_body.message = message
await self.user_client.me.send_mail.post(body=request_body)
# </SendMailSnippet>
# <MakeGraphCallSnippet>
async def make_graph_call(self):
# INSERT YOUR CODE HERE
return
# </MakeGraphCallSnippet>