Skip to content

Commit 124423a

Browse files
committed
Examples update
1 parent 8b45ca4 commit 124423a

File tree

9 files changed

+105
-24
lines changed

9 files changed

+105
-24
lines changed

examples/auth/with_client_secret.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Acquires a token by using application secret
3+
4+
The following options are supported:
5+
- utilize built in GraphClient.with_client_secret(tenant, client_id, client_secret, scopes, token_cache) method
6+
- or provide a custom callback function to GraphClient constructor as demonstrated below
7+
8+
https://learn.microsoft.com/en-us/entra/identity-platform/msal-authentication-flows#client-credentials
9+
"""
10+
import msal
11+
12+
from office365.graph_client import GraphClient
13+
from tests import test_client_id, test_client_secret, test_tenant
14+
15+
16+
def acquire_token():
17+
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant)
18+
app = msal.ConfidentialClientApplication(
19+
authority=authority_url,
20+
client_id=test_client_id,
21+
client_credential=test_client_secret,
22+
)
23+
return app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
24+
25+
26+
client = GraphClient(acquire_token)
27+
root_site = client.sites.root.get().execute_query()
28+
print(root_site.web_url)

office365/outlook/calendar/events/event.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from office365.entity_collection import EntityCollection
99
from office365.outlook.calendar.attendees.attendee import Attendee
1010
from office365.outlook.calendar.dateTimeTimeZone import DateTimeTimeZone
11+
from office365.outlook.calendar.response_status import ResponseStatus
1112
from office365.outlook.item import OutlookItem
1213
from office365.outlook.mail.attachments.collection import AttachmentCollection
1314
from office365.outlook.mail.item_body import ItemBody
@@ -195,6 +196,30 @@ def body_preview(self):
195196
"""The preview of the message associated with the event. It is in text format."""
196197
return self.properties.get("bodyPreview", None)
197198

199+
@property
200+
def reminder_minutes_before_start(self):
201+
# type: () -> Optional[int]
202+
"""The number of minutes before the event start time that the reminder alert occurs."""
203+
return self.properties.get("reminderMinutesBeforeStart", None)
204+
205+
@property
206+
def response_requested(self):
207+
# type: () -> Optional[bool]
208+
"""Default is true, which represents the organizer would like an invitee to send a response to the event."""
209+
return self.properties.get("responseRequested", None)
210+
211+
@property
212+
def response_status(self):
213+
# type: () -> Optional[str]
214+
"""Indicates the type of response sent in response to an event message."""
215+
return self.properties.get("responseStatus", ResponseStatus())
216+
217+
@property
218+
def series_master_id(self):
219+
# type: () -> Optional[str]
220+
"""The ID for the recurring series master item, if this event is part of a recurring series."""
221+
return self.properties.get("seriesMasterId", None)
222+
198223
@property
199224
def subject(self):
200225
# type: () -> Optional[str]
@@ -212,6 +237,26 @@ def location(self):
212237
"""The location of the event."""
213238
return self.properties.get("location", Location())
214239

240+
@property
241+
def transaction_id(self):
242+
# type: () -> Optional[str]
243+
"""
244+
A custom identifier specified by a client app for the server to avoid redundant POST operations in case of
245+
client retries to create the same event. This is useful when low network connectivity causes the client to
246+
time out before receiving a response from the server for the client's prior create-event request.
247+
After you set transactionId when creating an event, you cannot change transactionId in a subsequent update.
248+
This property is only returned in a response payload if an app has set it
249+
"""
250+
return self.properties.get("transactionId", None)
251+
252+
@property
253+
def type(self):
254+
# type: () -> Optional[str]
255+
"""
256+
The event type. Possible values are: singleInstance, occurrence, exception, seriesMaster
257+
"""
258+
return self.properties.get("type", None)
259+
215260
@property
216261
def web_link(self):
217262
# type: () -> Optional[str]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class ResponseStatus(ClientValue):
5+
"""Represents the response status of an attendee or organizer for a meeting request."""
6+
7+
def __init__(self, response=None):
8+
"""
9+
:type response: str
10+
"""
11+
self.response = response

tests/directory/test_governance.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from unittest import TestCase
22

33
from office365.graph_client import GraphClient
4-
from tests.graph_case import acquire_token_by_client_credentials
4+
from tests import test_client_id, test_client_secret, test_tenant
55

66

77
class TestIdentityGovernance(TestCase):
88
@classmethod
99
def setUpClass(cls):
10-
cls.client = GraphClient(acquire_token_by_client_credentials)
10+
cls.client = GraphClient.with_client_secret(
11+
test_tenant, test_client_id, test_client_secret
12+
)
1113

1214
def test1_list_app_consent_requests(self):
1315
result = (

tests/directory/test_identity.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from unittest import TestCase
22

33
from office365.graph_client import GraphClient
4-
from tests.graph_case import acquire_token_by_client_credentials
4+
from tests import test_client_id, test_client_secret, test_tenant
55

66

77
class TestIdentity(TestCase):
88
@classmethod
99
def setUpClass(cls):
10-
cls.client = GraphClient(acquire_token_by_client_credentials)
10+
cls.client = GraphClient.with_client_secret(
11+
test_tenant, test_client_id, test_client_secret
12+
)
1113

1214
def test1_list_identity_providers(self):
1315
result = self.client.identity.identity_providers.get().execute_query()

tests/directory/test_invitations.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from unittest import TestCase
22

33
from office365.graph_client import GraphClient
4-
from tests.graph_case import acquire_token_by_client_credentials
4+
from tests import test_client_id, test_client_secret, test_tenant
55

66

77
class TestInvitations(TestCase):
88
@classmethod
99
def setUpClass(cls):
10-
cls.client = GraphClient(acquire_token_by_client_credentials)
10+
cls.client = GraphClient.with_client_secret(
11+
test_tenant, test_client_id, test_client_secret
12+
)
1113

1214
def test1_create_invitation(self):
1315
invitation = self.client.invitations.create(

tests/directory/test_organization.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from unittest import TestCase
22

33
from office365.graph_client import GraphClient
4-
from tests.graph_case import acquire_token_by_client_credentials
4+
from tests import test_client_id, test_client_secret, test_tenant
55

66

77
class TestOrganization(TestCase):
88
@classmethod
99
def setUpClass(cls):
10-
cls.client = GraphClient(acquire_token_by_client_credentials)
10+
cls.client = GraphClient.with_client_secret(
11+
test_tenant, test_client_id, test_client_secret
12+
)
1113

1214
def test1_list(self):
1315
org = self.client.organization.get().execute_query()

tests/graph_case.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@ def acquire_token_by_username_password():
3030
return result
3131

3232

33-
def acquire_token_by_client_credentials():
34-
settings = load_settings()
35-
authority_url = "https://login.microsoftonline.com/{0}".format(
36-
settings.get("default", "tenant")
37-
)
38-
app = msal.ConfidentialClientApplication(
39-
authority=authority_url,
40-
client_id=settings.get("client_credentials", "client_id"),
41-
client_credential=settings.get("client_credentials", "client_secret"),
42-
)
43-
return app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
44-
45-
4633
class GraphTestCase(TestCase):
4734
"""Microsoft Graph specific test case base class"""
4835

tests/onedrive/test_term_store.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from office365.onedrive.termstore.sets.set import Set
66
from office365.onedrive.termstore.store import Store
77
from office365.onedrive.termstore.terms.term import Term
8-
from tests import test_root_site_url
9-
from tests.graph_case import GraphTestCase, acquire_token_by_client_credentials
8+
from tests import test_client_id, test_client_secret, test_root_site_url, test_tenant
9+
from tests.graph_case import GraphTestCase
1010

1111

1212
class TestTermStore(GraphTestCase):
@@ -18,7 +18,9 @@ class TestTermStore(GraphTestCase):
1818
@classmethod
1919
def setUpClass(cls):
2020
super(TestTermStore, cls).setUpClass()
21-
client = GraphClient(acquire_token_by_client_credentials)
21+
client = GraphClient.with_client_secret(
22+
test_tenant, test_client_id, test_client_secret
23+
)
2224
cls.target_store = client.sites.get_by_url(test_root_site_url).term_store
2325

2426
@classmethod

0 commit comments

Comments
 (0)