-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
67 lines (57 loc) · 2.53 KB
/
views.py
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
import os
import urllib2
import cgi
import webapp2
import httplib2
import jinja2
import json
from oauth2client.appengine import OAuth2Decorator
from oauth2client.client import AccessTokenRefreshError
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
decorator = OAuth2Decorator(client_id='968592128601.apps.googleusercontent.com',
client_secret='7ChU9MpHjd2MEU49qks0iPVU',
scope='https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
)
class LandingHandler(webapp2.RequestHandler):
"""
If the user isn't logged in, will ask them to login
else will redirect to their page
"""
@decorator.oauth_aware
def get(self):
if not decorator.has_credentials():
template_values = {
'url': decorator.authorize_url(),
}
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render(template_values))
else :
try :
http = decorator.http()
token = decorator.credentials.access_token
authorized =json.load(urllib2.urlopen('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' %token))
# todo
# Check that the authorized data matches the oauth2 client data
data = json.load(urllib2.urlopen('https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s' %token))
self.redirect('/%s' %data['id'])
except AccessTokenRefreshError:
template_values = {
'url': decorator.authorize_url(),
}
template = jinja_environment.get_template('templates/index.html')
self.response.out.write(template.render(template_values))
class UserHandler(webapp2.RequestHandler):
"""
Show the user's page
"""
@decorator.oauth_required
def get(self, userID):
http = decorator.http()
token = decorator.credentials.access_token
data = json.load(urllib2.urlopen('https://www.googleapis.com/oauth2/v1/userinfo?access_token=%s' %token))
if userID == data['id'] :
self.response.write('<img src="%s" /> Welcome %s id %s' %(data['picture'], data['name'], data['id']))
else:
self.error(401)
self.response.out.write('access denied')