<s> from urllib2 import Request , urlopen <EOL> import base64 <EOL> from . import oauth <EOL> from . error import TweepError <EOL> from . api import API <EOL> class AuthHandler ( object ) : <EOL> def apply_auth ( self , url , method , headers , parameters ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_username ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class OAuthHandler ( AuthHandler ) : <EOL> """<STR_LIT>""" <EOL> OAUTH_HOST = '<STR_LIT>' <EOL> OAUTH_ROOT = '<STR_LIT>' <EOL> def __init__ ( self , consumer_key , consumer_secret , callback = None , secure = False ) : <EOL> self . _consumer = oauth . OAuthConsumer ( consumer_key , consumer_secret ) <EOL> self . _sigmethod = oauth . OAuthSignatureMethod_HMAC_SHA1 ( ) <EOL> self . request_token = None <EOL> self . access_token = None <EOL> self . callback = callback <EOL> self . username = None <EOL> self . secure = secure <EOL> def _get_oauth_url ( self , endpoint , secure = False ) : <EOL> if self . secure or secure : <EOL> prefix = '<STR_LIT>' <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> return prefix + self . OAUTH_HOST + self . OAUTH_ROOT + endpoint <EOL> def apply_auth ( self , url , method , headers , parameters ) : <EOL> request = oauth . OAuthRequest . from_consumer_and_token ( <EOL> self . _consumer , http_url = url , http_method = method , <EOL> token = self . access_token , parameters = parameters <EOL> ) <EOL> request . sign_request ( self . _sigmethod , self . _consumer , self . access_token ) <EOL> headers . update ( request . to_header ( ) ) <EOL> def _get_request_token ( self ) : <EOL> try : <EOL> url = self . _get_oauth_url ( '<STR_LIT>' ) <EOL> request = oauth . OAuthRequest . from_consumer_and_token ( <EOL> self . _consumer , http_url = url , callback = self . callback <EOL> ) <EOL> request . sign_request ( self . _sigmethod , self . _consumer , None ) <EOL> resp = urlopen ( Request ( url , headers = request . to_header ( ) ) ) <EOL> return oauth . OAuthToken . from_string ( resp . read ( ) ) <EOL> except Exception , e : <EOL> raise TweepError ( e ) <EOL> def set_request_token ( self , key , secret ) : <EOL> self . request_token = oauth . OAuthToken ( key , secret ) <EOL> def set_access_token ( self , key , secret ) : <EOL> self . access_token = oauth . OAuthToken ( key , secret ) <EOL> def get_authorization_url ( self , signin_with_twitter = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . request_token = self . _get_request_token ( ) <EOL> if signin_with_twitter : <EOL> url = self . _get_oauth_url ( '<STR_LIT>' ) <EOL> else : <EOL> url = self . _get_oauth_url ( '<STR_LIT>' ) <EOL> request = oauth . OAuthRequest . from_token_and_callback ( <EOL> token = self . request_token , http_url = url <EOL> ) <EOL> return request . to_url ( ) <EOL> except Exception , e : <EOL> raise TweepError ( e ) <EOL> def get_access_token ( self , verifier = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> url = self . _get_oauth_url ( '<STR_LIT>' ) <EOL> request = oauth . OAuthRequest . from_consumer_and_token ( <EOL> self . _consumer , <EOL> token = self . request_token , http_url = url , <EOL> verifier = str ( verifier ) <EOL> ) <EOL> request . sign_request ( self . _sigmethod , self . _consumer , self . request_token ) <EOL> resp = urlopen ( Request ( url , headers = request . to_header ( ) ) ) <EOL> self . access_token = oauth . OAuthToken . from_string ( resp . read ( ) ) <EOL> return self . access_token <EOL> except Exception , e : <EOL> raise TweepError ( e ) <EOL> def get_xauth_access_token ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> url = self . _get_oauth_url ( '<STR_LIT>' , secure = True ) <EOL> request = oauth . OAuthRequest . from_consumer_and_token ( <EOL> oauth_consumer = self . _consumer , <EOL> http_method = '<STR_LIT:POST>' , http_url = url , <EOL> parameters = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : username , <EOL> '<STR_LIT>' : password <EOL> } <EOL> ) <EOL> request . sign_request ( self . _sigmethod , self . _consumer , None ) <EOL> resp = urlopen ( Request ( url , data = request . to_postdata ( ) ) ) <EOL> self . access_token = oauth . OAuthToken . from_string ( resp . read ( ) ) <EOL> return self . access_token <EOL> except Exception , e : <EOL> raise TweepError ( e ) <EOL> def get_username ( self ) : <EOL> if self . username is None : <EOL> api = API ( self ) <EOL> user = api . verify_credentials ( ) <EOL> if user : <EOL> self . username = user . screen_name <EOL> else : <EOL> raise TweepError ( "<STR_LIT>" ) <EOL> return self . username </s>
