<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import copy <EOL> import httplib2 <EOL> import logging <EOL> import oauth2 as oauth <EOL> import urllib <EOL> import urlparse <EOL> from oauth2client . anyjson import simplejson <EOL> from oauth2client . client import Credentials <EOL> from oauth2client . client import Flow <EOL> from oauth2client . client import Storage <EOL> try : <EOL> from urlparse import parse_qsl <EOL> except ImportError : <EOL> from cgi import parse_qsl <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class RequestError ( Error ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class MissingParameter ( Error ) : <EOL> pass <EOL> class CredentialsInvalidError ( Error ) : <EOL> pass <EOL> def _abstract ( ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _oauth_uri ( name , discovery , params ) : <EOL> """<STR_LIT>""" <EOL> if name not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise KeyError ( name ) <EOL> keys = discovery [ name ] [ '<STR_LIT>' ] . keys ( ) <EOL> query = { } <EOL> for key in keys : <EOL> if key in params : <EOL> query [ key ] = params [ key ] <EOL> return discovery [ name ] [ '<STR_LIT:url>' ] + '<STR_LIT:?>' + urllib . urlencode ( query ) <EOL> class OAuthCredentials ( Credentials ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , consumer , token , user_agent ) : <EOL> """<STR_LIT>""" <EOL> self . consumer = consumer <EOL> self . token = token <EOL> self . user_agent = user_agent <EOL> self . store = None <EOL> self . _invalid = False <EOL> @ property <EOL> def invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self , "<STR_LIT>" , False ) <EOL> def set_store ( self , store ) : <EOL> """<STR_LIT>""" <EOL> self . store = store <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> d = copy . copy ( self . __dict__ ) <EOL> del d [ '<STR_LIT:store>' ] <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( state ) <EOL> self . store = None <EOL> def authorize ( self , http ) : <EOL> """<STR_LIT>""" <EOL> request_orig = http . request <EOL> signer = oauth . SignatureMethod_HMAC_SHA1 ( ) <EOL> def new_request ( uri , method = '<STR_LIT:GET>' , body = None , headers = None , <EOL> redirections = httplib2 . DEFAULT_MAX_REDIRECTS , <EOL> connection_type = None ) : <EOL> """<STR_LIT>""" <EOL> response_code = <NUM_LIT> <EOL> http . follow_redirects = False <EOL> while response_code in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> req = oauth . Request . from_consumer_and_token ( <EOL> self . consumer , self . token , http_method = method , http_url = uri ) <EOL> req . sign_request ( signer , self . consumer , self . token ) <EOL> if headers is None : <EOL> headers = { } <EOL> headers . update ( req . to_header ( ) ) <EOL> if '<STR_LIT>' in headers : <EOL> headers [ '<STR_LIT>' ] = self . user_agent + '<STR_LIT:U+0020>' + headers [ '<STR_LIT>' ] <EOL> else : <EOL> headers [ '<STR_LIT>' ] = self . user_agent <EOL> resp , content = request_orig ( uri , method , body , headers , <EOL> redirections , connection_type ) <EOL> response_code = resp . status <EOL> if response_code in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> uri = resp [ '<STR_LIT:location>' ] <EOL> if response_code == <NUM_LIT> : <EOL> logging . info ( '<STR_LIT>' % content ) <EOL> self . _invalid = True <EOL> if self . store is not None : <EOL> self . store ( self ) <EOL> raise CredentialsInvalidError ( "<STR_LIT>" ) <EOL> return resp , content <EOL> http . request = new_request <EOL> return http <EOL> class TwoLeggedOAuthCredentials ( Credentials ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , consumer_key , consumer_secret , user_agent ) : <EOL> """<STR_LIT>""" <EOL> self . consumer = oauth . Consumer ( consumer_key , consumer_secret ) <EOL> self . user_agent = user_agent <EOL> self . store = None <EOL> self . _requestor = None <EOL> @ property <EOL> def invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def getrequestor ( self ) : <EOL> return self . _requestor <EOL> def setrequestor ( self , email ) : <EOL> self . _requestor = email <EOL> requestor = property ( getrequestor , setrequestor , None , <EOL> '<STR_LIT>' ) <EOL> def set_store ( self , store ) : <EOL> """<STR_LIT>""" <EOL> self . store = store <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> d = copy . copy ( self . __dict__ ) <EOL> del d [ '<STR_LIT:store>' ] <EOL> return d <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . __dict__ . update ( state ) <EOL> self . store = None <EOL> def authorize ( self , http ) : <EOL> """<STR_LIT>""" <EOL> request_orig = http . request <EOL> signer = oauth . SignatureMethod_HMAC_SHA1 ( ) <EOL> def new_request ( uri , method = '<STR_LIT:GET>' , body = None , headers = None , <EOL> redirections = httplib2 . DEFAULT_MAX_REDIRECTS , <EOL> connection_type = None ) : <EOL> """<STR_LIT>""" <EOL> response_code = <NUM_LIT> <EOL> http . follow_redirects = False <EOL> while response_code in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> if self . _requestor is None : <EOL> raise MissingParameter ( <EOL> '<STR_LIT>' ) <EOL> parsed = list ( urlparse . urlparse ( uri ) ) <EOL> q = parse_qsl ( parsed [ <NUM_LIT:4> ] ) <EOL> q . append ( ( '<STR_LIT>' , self . _requestor ) ) <EOL> parsed [ <NUM_LIT:4> ] = urllib . urlencode ( q ) <EOL> uri = urlparse . urlunparse ( parsed ) <EOL> req = oauth . Request . from_consumer_and_token ( <EOL> self . consumer , None , http_method = method , http_url = uri ) <EOL> req . sign_request ( signer , self . consumer , None ) <EOL> if headers is None : <EOL> headers = { } <EOL> headers . update ( req . to_header ( ) ) <EOL> if '<STR_LIT>' in headers : <EOL> headers [ '<STR_LIT>' ] = self . user_agent + '<STR_LIT:U+0020>' + headers [ '<STR_LIT>' ] <EOL> else : <EOL> headers [ '<STR_LIT>' ] = self . user_agent <EOL> resp , content = request_orig ( uri , method , body , headers , <EOL> redirections , connection_type ) <EOL> response_code = resp . status <EOL> if response_code in [ <NUM_LIT> , <NUM_LIT> ] : <EOL> uri = resp [ '<STR_LIT:location>' ] <EOL> if response_code == <NUM_LIT> : <EOL> logging . info ( '<STR_LIT>' % content ) <EOL> raise CredentialsInvalidError ( "<STR_LIT>" ) <EOL> return resp , content <EOL> http . request = new_request <EOL> return http <EOL> class FlowThreeLegged ( Flow ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , discovery , consumer_key , consumer_secret , user_agent , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . discovery = discovery <EOL> self . consumer_key = consumer_key <EOL> self . consumer_secret = consumer_secret <EOL> self . user_agent = user_agent <EOL> self . params = kwargs <EOL> self . request_token = { } <EOL> required = { } <EOL> for uriinfo in discovery . itervalues ( ) : <EOL> for name , value in uriinfo [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if value [ '<STR_LIT>' ] and not name . startswith ( '<STR_LIT>' ) : <EOL> required [ name ] = <NUM_LIT:1> <EOL> for key in required . iterkeys ( ) : <EOL> if key not in self . params : <EOL> raise MissingParameter ( '<STR_LIT>' % key ) <EOL> def step1_get_authorize_url ( self , oauth_callback = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> consumer = oauth . Consumer ( self . consumer_key , self . consumer_secret ) <EOL> client = oauth . Client ( consumer ) <EOL> headers = { <EOL> '<STR_LIT>' : self . user_agent , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> body = urllib . urlencode ( { '<STR_LIT>' : oauth_callback } ) <EOL> uri = _oauth_uri ( '<STR_LIT>' , self . discovery , self . params ) <EOL> resp , content = client . request ( uri , '<STR_LIT:POST>' , headers = headers , <EOL> body = body ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> logging . error ( '<STR_LIT>' , content ) <EOL> raise RequestError ( '<STR_LIT>' % resp [ '<STR_LIT:status>' ] ) <EOL> self . request_token = dict ( parse_qsl ( content ) ) <EOL> auth_params = copy . copy ( self . params ) <EOL> auth_params [ '<STR_LIT>' ] = self . request_token [ '<STR_LIT>' ] <EOL> return _oauth_uri ( '<STR_LIT>' , self . discovery , auth_params ) <EOL> def step2_exchange ( self , verifier ) : <EOL> """<STR_LIT>""" <EOL> if not ( isinstance ( verifier , str ) or isinstance ( verifier , unicode ) ) : <EOL> verifier = verifier [ '<STR_LIT>' ] <EOL> token = oauth . Token ( <EOL> self . request_token [ '<STR_LIT>' ] , <EOL> self . request_token [ '<STR_LIT>' ] ) <EOL> token . set_verifier ( verifier ) <EOL> consumer = oauth . Consumer ( self . consumer_key , self . consumer_secret ) <EOL> client = oauth . Client ( consumer , token ) <EOL> headers = { <EOL> '<STR_LIT>' : self . user_agent , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> uri = _oauth_uri ( '<STR_LIT>' , self . discovery , self . params ) <EOL> resp , content = client . request ( uri , '<STR_LIT:POST>' , headers = headers ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> logging . error ( '<STR_LIT>' , content ) <EOL> raise RequestError ( '<STR_LIT>' % resp [ '<STR_LIT:status>' ] ) <EOL> oauth_params = dict ( parse_qsl ( content ) ) <EOL> token = oauth . Token ( <EOL> oauth_params [ '<STR_LIT>' ] , <EOL> oauth_params [ '<STR_LIT>' ] ) <EOL> return OAuthCredentials ( consumer , token , self . user_agent ) </s>
