Skip to content

Commit 5ecc4cd

Browse files
committed
Converted to use Graph
1 parent 2568eb2 commit 5ecc4cd

6 files changed

Lines changed: 27 additions & 26 deletions

File tree

tutorial/authhelper.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
# The scopes required by the app
2323
scopes = [ 'openid',
2424
'offline_access',
25-
'https://outlook.office.com/mail.read',
26-
'https://outlook.office.com/calendars.read',
27-
'https://outlook.office.com/contacts.read' ]
25+
'User.Read',
26+
'Mail.Read',
27+
'Calendars.Read',
28+
'Contacts.Read' ]
2829

2930
def get_signin_url(redirect_uri):
3031
# Build the query parameters for the signin url

tutorial/outlookservice.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uuid
44
import json
55

6-
outlook_api_endpoint = 'https://outlook.office.com/api/v2.0{0}'
6+
graph_endpoint = 'https://graph.microsoft.com/v1.0{0}'
77

88
# Generic API Sending
99
def make_api_call(method, url, token, user_email, payload = None, parameters = None):
@@ -38,11 +38,11 @@ def make_api_call(method, url, token, user_email, payload = None, parameters = N
3838
return response
3939

4040
def get_me(access_token):
41-
get_me_url = outlook_api_endpoint.format('/Me')
41+
get_me_url = graph_endpoint.format('/me')
4242

4343
# Use OData query parameters to control the results
44-
# - Only return the DisplayName and EmailAddress fields
45-
query_parameters = {'$select': 'DisplayName,EmailAddress'}
44+
# - Only return the displayName and mail fields
45+
query_parameters = {'$select': 'displayName,mail'}
4646

4747
r = make_api_call('GET', get_me_url, access_token, "", parameters = query_parameters)
4848

@@ -52,15 +52,15 @@ def get_me(access_token):
5252
return "{0}: {1}".format(r.status_code, r.text)
5353

5454
def get_my_messages(access_token, user_email):
55-
get_messages_url = outlook_api_endpoint.format('/Me/MailFolders/Inbox/Messages')
55+
get_messages_url = graph_endpoint.format('/me/mailfolders/inbox/messages')
5656

5757
# Use OData query parameters to control the results
5858
# - Only first 10 results returned
5959
# - Only return the ReceivedDateTime, Subject, and From fields
6060
# - Sort the results by the ReceivedDateTime field in descending order
6161
query_parameters = {'$top': '10',
62-
'$select': 'ReceivedDateTime,Subject,From',
63-
'$orderby': 'ReceivedDateTime DESC'}
62+
'$select': 'receivedDateTime,subject,from',
63+
'$orderby': 'receivedDateTime DESC'}
6464

6565
r = make_api_call('GET', get_messages_url, access_token, user_email, parameters = query_parameters)
6666

@@ -70,15 +70,15 @@ def get_my_messages(access_token, user_email):
7070
return "{0}: {1}".format(r.status_code, r.text)
7171

7272
def get_my_events(access_token, user_email):
73-
get_events_url = outlook_api_endpoint.format('/Me/Events')
73+
get_events_url = graph_endpoint.format('/me/events')
7474

7575
# Use OData query parameters to control the results
7676
# - Only first 10 results returned
7777
# - Only return the Subject, Start, and End fields
7878
# - Sort the results by the Start field in ascending order
7979
query_parameters = {'$top': '10',
80-
'$select': 'Subject,Start,End',
81-
'$orderby': 'Start/DateTime ASC'}
80+
'$select': 'subject,start,end',
81+
'$orderby': 'start/dateTime ASC'}
8282

8383
r = make_api_call('GET', get_events_url, access_token, user_email, parameters = query_parameters)
8484

@@ -88,15 +88,15 @@ def get_my_events(access_token, user_email):
8888
return "{0}: {1}".format(r.status_code, r.text)
8989

9090
def get_my_contacts(access_token, user_email):
91-
get_contacts_url = outlook_api_endpoint.format('/Me/Contacts')
91+
get_contacts_url = graph_endpoint.format('/me/contacts')
9292

9393
# Use OData query parameters to control the results
9494
# - Only first 10 results returned
9595
# - Only return the GivenName, Surname, and EmailAddresses fields
9696
# - Sort the results by the GivenName field in ascending order
9797
query_parameters = {'$top': '10',
98-
'$select': 'GivenName,Surname,EmailAddresses',
99-
'$orderby': 'GivenName ASC'}
98+
'$select': 'givenName,surname,emailAddresses',
99+
'$orderby': 'givenName ASC'}
100100

101101
r = make_api_call('GET', get_contacts_url, access_token, user_email, parameters = query_parameters)
102102

tutorial/templates/tutorial/contacts.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ <h1>Your Contacts</h1>
1111

1212
{% for contact in contacts %}
1313
<tr>
14-
<td>{{ contact.GivenName }}</td>
15-
<td>{{ contact.Surname }}</td>
16-
<td>{{ contact.EmailAddresses.0.Address }}</td>
14+
<td>{{ contact.givenName }}</td>
15+
<td>{{ contact.surname }}</td>
16+
<td>{{ contact.emailAddresses.0.address }}</td>
1717
</tr>
1818
{% endfor %}
1919
</table>

tutorial/templates/tutorial/events.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ <h1>Your Events</h1>
1111

1212
{% for event in events %}
1313
<tr>
14-
<td>{{ event.Subject }}</td>
15-
<td>{{ event.Start.DateTime }} ({{ event.Start.TimeZone }})</td>
16-
<td>{{ event.End.DateTime }} ({{ event.End.TimeZone }})</td>
14+
<td>{{ event.subject }}</td>
15+
<td>{{ event.start.dateTime }} ({{ event.start.timeZone }})</td>
16+
<td>{{ event.end.dateTime }} ({{ event.end.timeZone }})</td>
1717
</tr>
1818
{% endfor %}
1919
</table>

tutorial/templates/tutorial/mail.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ <h1>Your Email</h1>
1111

1212
{% for message in messages %}
1313
<tr>
14-
<td>{{ message.From.EmailAddress.Name }}</td>
15-
<td>{{ message.Subject }}</td>
16-
<td>{{ message.ReceivedDateTime }}</td>
14+
<td>{{ message.from.emailAddress.name }}</td>
15+
<td>{{ message.subject }}</td>
16+
<td>{{ message.receivedDateTime }}</td>
1717
</tr>
1818
{% endfor %}
1919
</table>

tutorial/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def gettoken(request):
3333
request.session['access_token'] = access_token
3434
request.session['refresh_token'] = refresh_token
3535
request.session['token_expires'] = expiration
36-
request.session['user_email'] = user['EmailAddress']
36+
request.session['user_email'] = user['mail']
3737

3838
return HttpResponseRedirect(reverse('tutorial:mail'))
3939

0 commit comments

Comments
 (0)