Skip to content
This repository was archived by the owner on Mar 13, 2020. It is now read-only.

Commit 20a9ca9

Browse files
committed
Merge branch 'refs/heads/outlookv2'
2 parents de86440 + a4ec6cb commit 20a9ca9

6 files changed

Lines changed: 106 additions & 2 deletions

File tree

tutorial/authhelper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
# The scopes required by the app
2222
scopes = [ 'openid',
23-
'https://outlook.office.com/mail.read' ]
23+
'https://outlook.office.com/mail.read',
24+
'https://outlook.office.com/calendars.read',
25+
'https://outlook.office.com/contacts.read' ]
2426

2527
def get_signin_url(redirect_uri):
2628
# Build the query parameters for the signin url

tutorial/outlookservice.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,42 @@ def get_my_messages(access_token, user_email):
5454
return r.json()
5555
else:
5656
return "{0}: {1}".format(r.status_code, r.text)
57+
58+
def get_my_events(access_token, user_email):
59+
get_events_url = outlook_api_endpoint.format('/Me/Events')
60+
61+
# Use OData query parameters to control the results
62+
# - Only first 10 results returned
63+
# - Only return the Subject, Start, and End fields
64+
# - Sort the results by the Start field in ascending order
65+
query_parameters = {'$top': '10',
66+
'$select': 'Subject,Start,End',
67+
'$orderby': 'Start ASC'}
68+
69+
r = make_api_call('GET', get_events_url, access_token, user_email, parameters = query_parameters)
70+
71+
if (r.status_code == requests.codes.ok):
72+
return r.json()
73+
else:
74+
return "{0}: {1}".format(r.status_code, r.text)
75+
76+
def get_my_contacts(access_token, user_email):
77+
get_contacts_url = outlook_api_endpoint.format('/Me/Contacts')
78+
79+
# Use OData query parameters to control the results
80+
# - Only first 10 results returned
81+
# - Only return the GivenName, Surname, and EmailAddresses fields
82+
# - Sort the results by the GivenName field in ascending order
83+
query_parameters = {'$top': '10',
84+
'$select': 'GivenName,Surname,EmailAddresses',
85+
'$orderby': 'GivenName ASC'}
86+
87+
r = make_api_call('GET', get_contacts_url, access_token, user_email, parameters = query_parameters)
88+
89+
if (r.status_code == requests.codes.ok):
90+
return r.json()
91+
else:
92+
return "{0}: {1}".format(r.status_code, r.text)
5793

5894
# MIT License:
5995

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<body>
3+
<h1>Your Contacts</h1>
4+
<table width="100%" border="1">
5+
<tr>
6+
<th>First Name</th>
7+
<th>Last Name</th>
8+
<th>Email Address</th>
9+
</tr>
10+
11+
{% for contact in contacts %}
12+
<tr>
13+
<td>{{ contact.GivenName }}</td>
14+
<td>{{ contact.Surname }}</td>
15+
<td>{{ contact.EmailAddresses.0.Address }}</td>
16+
</tr>
17+
{% endfor %}
18+
</table>
19+
</body>
20+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<body>
3+
<h1>Your Events</h1>
4+
<table width="100%" border="1">
5+
<tr>
6+
<th>Subject</th>
7+
<th>Start</th>
8+
<th>End</th>
9+
</tr>
10+
11+
{% for event in events %}
12+
<tr>
13+
<td>{{ event.Subject }}</td>
14+
<td>{{ event.Start }}</td>
15+
<td>{{ event.End }}</td>
16+
</tr>
17+
{% endfor %}
18+
</table>
19+
</body>
20+
</html>

tutorial/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
url(r'^gettoken/$', views.gettoken, name='gettoken'),
1212
# Mail view ('/tutorial/mail/')
1313
url(r'^mail/$', views.mail, name='mail'),
14+
# Events view ('/tutorial/events/')
15+
url(r'^events/$', views.events, name='events'),
16+
# Contacts view ('/tutorial/contacts/')
17+
url(r'^contacts/$', views.contacts, name='contacts'),
1418
)
1519

1620
# MIT License:

tutorial/views.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.http import HttpResponse, HttpResponseRedirect
44
from django.core.urlresolvers import reverse
55
from tutorial.authhelper import get_signin_url, get_token_from_code, get_user_email_from_id_token
6-
from tutorial.outlookservice import get_my_messages
6+
from tutorial.outlookservice import get_my_messages, get_my_events, get_my_contacts
77

88
# Create your views here.
99

@@ -35,6 +35,28 @@ def mail(request):
3535
context = { 'messages': messages['value'] }
3636
return render(request, 'tutorial/mail.html', context)
3737

38+
def events(request):
39+
access_token = request.session['access_token']
40+
user_email = request.session['user_email']
41+
# If there is no token in the session, redirect to home
42+
if not access_token:
43+
return HttpResponseRedirect(reverse('tutorial:home'))
44+
else:
45+
events = get_my_events(access_token, user_email)
46+
context = { 'events': events['value'] }
47+
return render(request, 'tutorial/events.html', context)
48+
49+
def contacts(request):
50+
access_token = request.session['access_token']
51+
user_email = request.session['user_email']
52+
# If there is no token in the session, redirect to home
53+
if not access_token:
54+
return HttpResponseRedirect(reverse('tutorial:home'))
55+
else:
56+
contacts = get_my_contacts(access_token, user_email)
57+
context = { 'contacts': contacts['value'] }
58+
return render(request, 'tutorial/contacts.html', context)
59+
3860
# MIT License:
3961

4062
# Permission is hereby granted, free of charge, to any person obtaining

0 commit comments

Comments
 (0)