<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import utils <EOL> from demos import demo_services <EOL> import random <EOL> import string <EOL> import httplib2 <EOL> import json <EOL> import logging <EOL> from apiclient . discovery import build <EOL> from apiclient . errors import HttpError <EOL> from apiclient . errors import UnknownApiNameOrVersion <EOL> from google . appengine . ext import ndb <EOL> from oauth2client . client import AccessTokenRefreshError <EOL> from oauth2client . client import flow_from_clientsecrets <EOL> from oauth2client . client import FlowExchangeError <EOL> from oauth2client . appengine import StorageByKeyName <EOL> def get_credentials ( gplus_id , test ) : <EOL> """<STR_LIT>""" <EOL> if test is not None : <EOL> storage = StorageByKeyName ( utils . TestUser , gplus_id , "<STR_LIT>" ) <EOL> else : <EOL> storage = StorageByKeyName ( utils . User , gplus_id , "<STR_LIT>" ) <EOL> credentials = storage . get ( ) <EOL> return credentials <EOL> def store_credentials ( gplus_id , test , credentials ) : <EOL> """<STR_LIT>""" <EOL> if test is not None : <EOL> storage = StorageByKeyName ( utils . TestUser , gplus_id , "<STR_LIT>" ) <EOL> else : <EOL> storage = StorageByKeyName ( utils . User , gplus_id , "<STR_LIT>" ) <EOL> storage . put ( credentials ) <EOL> def get_auth_service ( gplus_id , test , api = "<STR_LIT>" , version = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if test is not None and api == "<STR_LIT>" and version == "<STR_LIT>" : <EOL> discovery_service_url = utils . discovery_service_url <EOL> else : <EOL> discovery_service_url = None <EOL> credentials = get_credentials ( gplus_id , test ) <EOL> if credentials is None : <EOL> return None <EOL> http = httplib2 . Http ( ) <EOL> http = credentials . authorize ( http ) <EOL> http . timeout = <NUM_LIT> <EOL> if discovery_service_url is None : <EOL> service = build ( api , version , http = http ) <EOL> else : <EOL> service = build ( api , version , http = http , discoveryServiceUrl = discovery_service_url ) <EOL> return service <EOL> def _disconnect ( gplus_id , test ) : <EOL> """<STR_LIT>""" <EOL> store_credentials ( gplus_id , test , None ) <EOL> class ConnectHandler ( utils . BaseHandler ) : <EOL> def post ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . response . content_type = "<STR_LIT:application/json>" <EOL> state = self . request . get ( "<STR_LIT:state>" ) <EOL> gplus_id = self . request . get ( "<STR_LIT>" ) <EOL> code = self . request . body <EOL> if state != self . session . get ( "<STR_LIT:state>" ) : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> try : <EOL> oauth_flow = flow_from_clientsecrets ( '<STR_LIT>' , scope = '<STR_LIT>' ) <EOL> oauth_flow . redirect_uri = '<STR_LIT>' <EOL> credentials = oauth_flow . step2_exchange ( code ) <EOL> except FlowExchangeError : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( <EOL> utils . createError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ) <EOL> return <EOL> access_token = credentials . access_token <EOL> url = ( "<STR_LIT>" % access_token ) <EOL> h = httplib2 . Http ( ) <EOL> result = json . loads ( h . request ( url , '<STR_LIT:GET>' ) [ <NUM_LIT:1> ] ) <EOL> if result . get ( "<STR_LIT:error>" ) is not None : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( json . dumps ( result . get ( "<STR_LIT:error>" ) ) ) <EOL> return <EOL> if result [ "<STR_LIT>" ] != gplus_id : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( <EOL> utils . createError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ) <EOL> return <EOL> if result [ '<STR_LIT>' ] != utils . CLIENT_ID : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( <EOL> utils . createError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ) <EOL> return <EOL> self . session [ "<STR_LIT>" ] = gplus_id <EOL> stored_credentials = get_credentials ( gplus_id , test ) <EOL> new_user = False <EOL> if stored_credentials is None : <EOL> new_user = True <EOL> store_credentials ( gplus_id , test , credentials ) <EOL> credentials = get_credentials ( gplus_id , test ) <EOL> if credentials . refresh_token is None : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( <EOL> utils . createError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ) <EOL> return <EOL> try : <EOL> service = get_auth_service ( gplus_id , test ) <EOL> plus_service = get_auth_service ( gplus_id , test , "<STR_LIT>" , "<STR_LIT>" ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except UnknownApiNameOrVersion : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> try : <EOL> result = plus_service . people ( ) . get ( userId = "<STR_LIT>" , fields = "<STR_LIT>" ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> if test is not None : <EOL> user = ndb . Key ( "<STR_LIT>" , gplus_id ) . get ( ) <EOL> else : <EOL> user = ndb . Key ( "<STR_LIT>" , gplus_id ) . get ( ) <EOL> user . displayName = result [ "<STR_LIT>" ] <EOL> user . imageUrl = result [ "<STR_LIT:image>" ] [ "<STR_LIT:url>" ] <EOL> user . put ( ) <EOL> try : <EOL> result = plus_service . people ( ) . list ( userId = "<STR_LIT>" , collection = "<STR_LIT>" , maxResults = <NUM_LIT:100> , orderBy = "<STR_LIT>" , fields = "<STR_LIT>" ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> friends = [ ] <EOL> if "<STR_LIT>" in result : <EOL> for item in result [ "<STR_LIT>" ] : <EOL> friends . append ( item [ "<STR_LIT:id>" ] ) <EOL> user . friends = friends <EOL> user . put ( ) <EOL> try : <EOL> result = service . contacts ( ) . list ( ) . execute ( ) <EOL> if "<STR_LIT>" in result : <EOL> for contact in result [ "<STR_LIT>" ] : <EOL> del_result = service . contacts ( ) . delete ( id = contact [ "<STR_LIT:id>" ] ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> contacts = [ ] <EOL> for demo_service in demo_services : <EOL> if hasattr ( demo_service , "<STR_LIT>" ) : <EOL> contacts . extend ( demo_service . CONTACTS ) <EOL> for contact in contacts : <EOL> try : <EOL> result = service . contacts ( ) . insert ( body = contact ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = service . subscriptions ( ) . list ( ) . execute ( ) <EOL> if "<STR_LIT>" in result : <EOL> for subscription in result [ "<STR_LIT>" ] : <EOL> del_result = service . subscriptions ( ) . delete ( id = subscription [ "<STR_LIT:id>" ] ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> verifyToken = '<STR_LIT>' . join ( random . choice ( string . ascii_letters + string . digits ) for x in range ( <NUM_LIT:32> ) ) <EOL> user . verifyToken = verifyToken <EOL> user . put ( ) <EOL> body = { } <EOL> body [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> body [ "<STR_LIT>" ] = gplus_id <EOL> body [ "<STR_LIT>" ] = verifyToken <EOL> body [ "<STR_LIT>" ] = utils . base_url + ( "<STR_LIT>" if test is None else "<STR_LIT>" ) + "<STR_LIT>" <EOL> try : <EOL> result = service . subscriptions ( ) . insert ( body = body ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> body = { } <EOL> body [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> body [ "<STR_LIT>" ] = gplus_id <EOL> body [ "<STR_LIT>" ] = verifyToken <EOL> body [ "<STR_LIT>" ] = utils . base_url + ( "<STR_LIT>" if test is None else "<STR_LIT>" ) + "<STR_LIT>" <EOL> try : <EOL> result = service . subscriptions ( ) . insert ( body = body ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> if not new_user : <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( utils . createMessage ( "<STR_LIT>" ) ) <EOL> return <EOL> welcomes = [ ] <EOL> for demo_service in demo_services : <EOL> if hasattr ( demo_service , "<STR_LIT>" ) : <EOL> welcomes . extend ( demo_service . WELCOMES ) <EOL> for welcome in welcomes : <EOL> try : <EOL> result = service . timeline ( ) . insert ( body = welcome ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> _disconnect ( gplus_id , test ) <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( utils . createMessage ( "<STR_LIT>" ) ) <EOL> class DisconnectHandler ( utils . BaseHandler ) : <EOL> def post ( self , test ) : <EOL> """<STR_LIT>""" <EOL> self . response . content_type = "<STR_LIT:application/json>" <EOL> gplus_id = self . session . get ( "<STR_LIT>" ) <EOL> credentials = get_credentials ( gplus_id , test ) <EOL> if credentials is None : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> try : <EOL> service = get_auth_service ( gplus_id , test ) <EOL> except AccessTokenRefreshError : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except UnknownApiNameOrVersion : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> try : <EOL> result = service . contacts ( ) . list ( ) . execute ( ) <EOL> if "<STR_LIT>" in result : <EOL> for contact in result [ "<STR_LIT>" ] : <EOL> del_result = service . contacts ( ) . delete ( id = contact [ "<STR_LIT:id>" ] ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> try : <EOL> result = service . subscriptions ( ) . list ( ) . execute ( ) <EOL> if "<STR_LIT>" in result : <EOL> for subscription in result [ "<STR_LIT>" ] : <EOL> del_result = service . subscriptions ( ) . delete ( id = subscription [ "<STR_LIT:id>" ] ) . execute ( ) <EOL> except AccessTokenRefreshError : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> return <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> access_token = credentials . access_token <EOL> url = "<STR_LIT>" % access_token <EOL> h = httplib2 . Http ( ) <EOL> try : <EOL> result = h . request ( url , "<STR_LIT:GET>" ) [ <NUM_LIT:0> ] <EOL> if result [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> self . response . status = <NUM_LIT:200> <EOL> self . response . out . write ( utils . createMessage ( "<STR_LIT>" ) ) <EOL> else : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" ) ) <EOL> except HttpError as e : <EOL> self . response . status = <NUM_LIT> <EOL> self . response . out . write ( utils . createError ( <NUM_LIT> , "<STR_LIT>" % e ) ) <EOL> return <EOL> if test is not None : <EOL> ndb . Key ( "<STR_LIT>" , gplus_id ) . delete ( ) <EOL> else : <EOL> ndb . Key ( "<STR_LIT>" , gplus_id ) . delete ( ) <EOL> AUTH_ROUTES = [ <EOL> ( r"<STR_LIT>" , ConnectHandler ) , <EOL> ( r"<STR_LIT>" , DisconnectHandler ) <EOL> ] </s>
