<s> import webapp2 <EOL> from urllib import urlencode <EOL> import json , urllib2 <EOL> from secret import client_id , client_secret <EOL> import config <EOL> class AuthRedirector ( webapp2 . RequestHandler ) : <EOL> def get ( self ) : <EOL> args = self . request . GET <EOL> args [ "<STR_LIT>" ] = client_id <EOL> args [ "<STR_LIT>" ] = config . auth_redir_uri <EOL> url = "<STR_LIT>" + urlencode ( args ) <EOL> self . response . location = url <EOL> self . response . status_int = <NUM_LIT> <EOL> def query_json ( url , data ) : <EOL> """<STR_LIT>""" <EOL> if not ( data is str ) : <EOL> data = urlencode ( data ) <EOL> try : <EOL> return json . loads ( urllib2 . urlopen ( url , data ) . read ( ) ) <EOL> except urllib2 . HTTPError as e : <EOL> return json . loads ( e . read ( ) ) <EOL> def json_compactify ( data ) : <EOL> return json . dumps ( data , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> class AuthCallback ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> state = self . request . get ( "<STR_LIT:state>" ) <EOL> code = self . request . get ( "<STR_LIT:code>" ) <EOL> error = self . request . get ( "<STR_LIT:error>" ) <EOL> q = { <EOL> "<STR_LIT:code>" : code , <EOL> "<STR_LIT>" : client_id , <EOL> "<STR_LIT>" : client_secret , <EOL> "<STR_LIT>" : config . auth_redir_uri , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> result = query_json ( "<STR_LIT>" , q ) <EOL> url = ( config . auth_success_page if "<STR_LIT>" in result <EOL> else config . auth_failure_page ) + "<STR_LIT:#>" + urlencode ( result ) <EOL> self . response . location = url <EOL> self . response . status_int = <NUM_LIT> <EOL> class AuthRefresh ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> refresh_token = self . request . get ( "<STR_LIT>" ) <EOL> if not refresh_token : <EOL> self . response . status_int = <NUM_LIT> <EOL> return <EOL> q = { <EOL> "<STR_LIT>" : refresh_token , <EOL> "<STR_LIT>" : client_id , <EOL> "<STR_LIT>" : client_secret , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> result = query_json ( "<STR_LIT>" , q ) <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = "<STR_LIT>" <EOL> self . response . write ( json_compactify ( result ) ) <EOL> application = webapp2 . WSGIApplication ( [ <EOL> ( '<STR_LIT>' , AuthRedirector ) , <EOL> ( '<STR_LIT>' , AuthCallback ) , <EOL> ( '<STR_LIT>' , AuthRefresh ) , <EOL> ] , debug = True ) </s>
