<s> """<STR_LIT>""" <EOL> import httplib <EOL> import time <EOL> import oauth . oauth as oauth <EOL> SERVER = '<STR_LIT:localhost>' <EOL> PORT = <NUM_LIT> <EOL> REQUEST_TOKEN_URL = '<STR_LIT>' <EOL> ACCESS_TOKEN_URL = '<STR_LIT>' <EOL> AUTHORIZATION_URL = '<STR_LIT>' <EOL> CALLBACK_URL = '<STR_LIT>' <EOL> RESOURCE_URL = '<STR_LIT>' <EOL> CONSUMER_KEY = '<STR_LIT:key>' <EOL> CONSUMER_SECRET = '<STR_LIT>' <EOL> class SimpleOAuthClient ( oauth . OAuthClient ) : <EOL> def __init__ ( self , server , port = httplib . HTTP_PORT , request_token_url = '<STR_LIT>' , access_token_url = '<STR_LIT>' , authorization_url = '<STR_LIT>' ) : <EOL> self . server = server <EOL> self . port = port <EOL> self . request_token_url = request_token_url <EOL> self . access_token_url = access_token_url <EOL> self . authorization_url = authorization_url <EOL> self . connection = httplib . HTTPConnection ( "<STR_LIT>" % ( self . server , self . port ) ) <EOL> def fetch_request_token ( self , oauth_request ) : <EOL> self . connection . request ( oauth_request . http_method , self . request_token_url , headers = oauth_request . to_header ( ) ) <EOL> response = self . connection . getresponse ( ) <EOL> return oauth . OAuthToken . from_string ( response . read ( ) ) <EOL> def fetch_access_token ( self , oauth_request ) : <EOL> self . connection . request ( oauth_request . http_method , self . access_token_url , headers = oauth_request . to_header ( ) ) <EOL> response = self . connection . getresponse ( ) <EOL> return oauth . OAuthToken . from_string ( response . read ( ) ) <EOL> def authorize_token ( self , oauth_request ) : <EOL> self . connection . request ( oauth_request . http_method , oauth_request . to_url ( ) ) <EOL> response = self . connection . getresponse ( ) <EOL> return response . read ( ) <EOL> def access_resource ( self , oauth_request ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> self . connection . request ( '<STR_LIT:POST>' , RESOURCE_URL , body = oauth_request . to_postdata ( ) , headers = headers ) <EOL> response = self . connection . getresponse ( ) <EOL> return response . read ( ) <EOL> def run_example ( ) : <EOL> print '<STR_LIT>' <EOL> client = SimpleOAuthClient ( SERVER , PORT , REQUEST_TOKEN_URL , ACCESS_TOKEN_URL , AUTHORIZATION_URL ) <EOL> consumer = oauth . OAuthConsumer ( CONSUMER_KEY , CONSUMER_SECRET ) <EOL> signature_method_plaintext = oauth . OAuthSignatureMethod_PLAINTEXT ( ) <EOL> signature_method_hmac_sha1 = oauth . OAuthSignatureMethod_HMAC_SHA1 ( ) <EOL> pause ( ) <EOL> print '<STR_LIT>' <EOL> pause ( ) <EOL> oauth_request = oauth . OAuthRequest . from_consumer_and_token ( consumer , callback = CALLBACK_URL , http_url = client . request_token_url ) <EOL> oauth_request . sign_request ( signature_method_plaintext , consumer , None ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( oauth_request . parameters ) <EOL> pause ( ) <EOL> token = client . fetch_request_token ( oauth_request ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( token . key ) <EOL> print '<STR_LIT>' % str ( token . secret ) <EOL> print '<STR_LIT>' % str ( token . callback_confirmed ) <EOL> pause ( ) <EOL> print '<STR_LIT>' <EOL> pause ( ) <EOL> oauth_request = oauth . OAuthRequest . from_token_and_callback ( token = token , http_url = client . authorization_url ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( oauth_request . parameters ) <EOL> pause ( ) <EOL> response = client . authorize_token ( oauth_request ) <EOL> print '<STR_LIT>' <EOL> print response <EOL> import urlparse , cgi <EOL> query = urlparse . urlparse ( response ) [ <NUM_LIT:4> ] <EOL> params = cgi . parse_qs ( query , keep_blank_values = False ) <EOL> verifier = params [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' % verifier <EOL> pause ( ) <EOL> print '<STR_LIT>' <EOL> pause ( ) <EOL> oauth_request = oauth . OAuthRequest . from_consumer_and_token ( consumer , token = token , verifier = verifier , http_url = client . access_token_url ) <EOL> oauth_request . sign_request ( signature_method_plaintext , consumer , token ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( oauth_request . parameters ) <EOL> pause ( ) <EOL> token = client . fetch_access_token ( oauth_request ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( token . key ) <EOL> print '<STR_LIT>' % str ( token . secret ) <EOL> pause ( ) <EOL> print '<STR_LIT>' <EOL> pause ( ) <EOL> parameters = { '<STR_LIT:file>' : '<STR_LIT>' , '<STR_LIT:size>' : '<STR_LIT>' } <EOL> oauth_request = oauth . OAuthRequest . from_consumer_and_token ( consumer , token = token , http_method = '<STR_LIT:POST>' , http_url = RESOURCE_URL , parameters = parameters ) <EOL> oauth_request . sign_request ( signature_method_hmac_sha1 , consumer , token ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % str ( oauth_request . parameters ) <EOL> pause ( ) <EOL> params = client . access_resource ( oauth_request ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' % params <EOL> pause ( ) <EOL> def pause ( ) : <EOL> print '<STR_LIT>' <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_example ( ) <EOL> print '<STR_LIT>' </s>
