3030import os
3131import pickle
3232
33- from apiclient . discovery import build
34- from oauth2client . appengine import oauth2decorator_from_clientsecrets
35- from oauth2client . client import AccessTokenRefreshError
33+ from apiclient import discovery
34+ from oauth2client import appengine
35+ from oauth2client import client
3636from google .appengine .api import memcache
37- from google .appengine .ext import webapp
38- from google .appengine .ext .webapp import template
39- from google .appengine .ext .webapp .util import run_wsgi_app
4037
38+ import webapp2
39+ import jinja2
40+
41+
42+ JINJA_ENVIRONMENT = jinja2 .Environment (
43+ loader = jinja2 .FileSystemLoader (os .path .dirname (__file__ )),
44+ autoescape = True ,
45+ extensions = ['jinja2.ext.autoescape' ])
4146
4247# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
4348# application, including client_id and client_secret, which are found
6368
6469
6570http = httplib2 .Http (memcache )
66- service = build ("plus" , "v1" , http = http )
67- decorator = oauth2decorator_from_clientsecrets (
71+ service = discovery . build ("plus" , "v1" , http = http )
72+ decorator = appengine . oauth2decorator_from_clientsecrets (
6873 CLIENT_SECRETS ,
6974 scope = 'https://www.googleapis.com/auth/plus.me' ,
7075 message = MISSING_CLIENT_SECRETS_MESSAGE )
7176
72- class MainHandler (webapp .RequestHandler ):
77+ class MainHandler (webapp2 .RequestHandler ):
7378
7479 @decorator .oauth_aware
7580 def get (self ):
76- path = os .path .join (os .path .dirname (__file__ ), 'grant.html' )
7781 variables = {
7882 'url' : decorator .authorize_url (),
7983 'has_credentials' : decorator .has_credentials ()
8084 }
81- self .response .out .write (template .render (path , variables ))
85+ template = JINJA_ENVIRONMENT .get_template ('grant.html' )
86+ self .response .write (template .render (variables ))
8287
8388
84- class AboutHandler (webapp .RequestHandler ):
89+ class AboutHandler (webapp2 .RequestHandler ):
8590
8691 @decorator .oauth_required
8792 def get (self ):
@@ -90,22 +95,16 @@ def get(self):
9095 user = service .people ().get (userId = 'me' ).execute (http = http )
9196 text = 'Hello, %s!' % user ['displayName' ]
9297
93- path = os . path . join ( os . path . dirname ( __file__ ), 'welcome.html' )
94- self .response .out . write (template .render (path , {'text' : text }))
95- except AccessTokenRefreshError :
98+ template = JINJA_ENVIRONMENT . get_template ( 'welcome.html' )
99+ self .response .write (template .render ({'text' : text }))
100+ except client . AccessTokenRefreshError :
96101 self .redirect ('/' )
97102
98103
99- def main ():
100- application = webapp .WSGIApplication (
101- [
102- ('/' , MainHandler ),
103- ('/about' , AboutHandler ),
104- (decorator .callback_path , decorator .callback_handler ()),
105- ],
106- debug = True )
107- run_wsgi_app (application )
108-
109-
110- if __name__ == '__main__' :
111- main ()
104+ app = webapp2 .WSGIApplication (
105+ [
106+ ('/' , MainHandler ),
107+ ('/about' , AboutHandler ),
108+ (decorator .callback_path , decorator .callback_handler ()),
109+ ],
110+ debug = True )
0 commit comments