p@21: """ p@21: The MIT License p@21: p@21: Copyright (c) 2007 Leah Culver p@21: p@21: Permission is hereby granted, free of charge, to any person obtaining a copy p@21: of this software and associated documentation files (the "Software"), to deal p@21: in the Software without restriction, including without limitation the rights p@21: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell p@21: copies of the Software, and to permit persons to whom the Software is p@21: furnished to do so, subject to the following conditions: p@21: p@21: The above copyright notice and this permission notice shall be included in p@21: all copies or substantial portions of the Software. p@21: p@21: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR p@21: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, p@21: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE p@21: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER p@21: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, p@21: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN p@21: THE SOFTWARE. p@21: p@21: Example consumer. This is not recommended for production. p@21: Instead, you'll want to create your own subclass of OAuthClient p@21: or find one that works with your web framework. p@21: """ p@21: p@21: import httplib p@21: import time p@21: import oauth.oauth as oauth p@21: p@21: # settings for the local test consumer p@21: SERVER = 'localhost' p@21: PORT = 8080 p@21: p@21: # fake urls for the test server (matches ones in server.py) p@21: REQUEST_TOKEN_URL = 'https://photos.example.net/request_token' p@21: ACCESS_TOKEN_URL = 'https://photos.example.net/access_token' p@21: AUTHORIZATION_URL = 'https://photos.example.net/authorize' p@21: CALLBACK_URL = 'http://printer.example.com/request_token_ready' p@21: RESOURCE_URL = 'http://photos.example.net/photos' p@21: p@21: # key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore p@21: CONSUMER_KEY = 'key' p@21: CONSUMER_SECRET = 'secret' p@21: p@21: # example client using httplib with headers p@21: class SimpleOAuthClient(oauth.OAuthClient): p@21: p@21: def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''): p@21: self.server = server p@21: self.port = port p@21: self.request_token_url = request_token_url p@21: self.access_token_url = access_token_url p@21: self.authorization_url = authorization_url p@21: self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port)) p@21: p@21: def fetch_request_token(self, oauth_request): p@21: # via headers p@21: # -> OAuthToken p@21: self.connection.request(oauth_request.http_method, self.request_token_url, headers=oauth_request.to_header()) p@21: response = self.connection.getresponse() p@21: return oauth.OAuthToken.from_string(response.read()) p@21: p@21: def fetch_access_token(self, oauth_request): p@21: # via headers p@21: # -> OAuthToken p@21: self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header()) p@21: response = self.connection.getresponse() p@21: return oauth.OAuthToken.from_string(response.read()) p@21: p@21: def authorize_token(self, oauth_request): p@21: # via url p@21: # -> typically just some okay response p@21: self.connection.request(oauth_request.http_method, oauth_request.to_url()) p@21: response = self.connection.getresponse() p@21: return response.read() p@21: p@21: def access_resource(self, oauth_request): p@21: # via post body p@21: # -> some protected resources p@21: headers = {'Content-Type' :'application/x-www-form-urlencoded'} p@21: self.connection.request('POST', RESOURCE_URL, body=oauth_request.to_postdata(), headers=headers) p@21: response = self.connection.getresponse() p@21: return response.read() p@21: p@21: def run_example(): p@21: p@21: # setup p@21: print '** OAuth Python Library Example **' p@21: client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL) p@21: consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) p@21: signature_method_plaintext = oauth.OAuthSignatureMethod_PLAINTEXT() p@21: signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() p@21: pause() p@21: p@21: # get request token p@21: print '* Obtain a request token ...' p@21: pause() p@21: oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, callback=CALLBACK_URL, http_url=client.request_token_url) p@21: oauth_request.sign_request(signature_method_plaintext, consumer, None) p@21: print 'REQUEST (via headers)' p@21: print 'parameters: %s' % str(oauth_request.parameters) p@21: pause() p@21: token = client.fetch_request_token(oauth_request) p@21: print 'GOT' p@21: print 'key: %s' % str(token.key) p@21: print 'secret: %s' % str(token.secret) p@21: print 'callback confirmed? %s' % str(token.callback_confirmed) p@21: pause() p@21: p@21: print '* Authorize the request token ...' p@21: pause() p@21: oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url) p@21: print 'REQUEST (via url query string)' p@21: print 'parameters: %s' % str(oauth_request.parameters) p@21: pause() p@21: # this will actually occur only on some callback p@21: response = client.authorize_token(oauth_request) p@21: print 'GOT' p@21: print response p@21: # sad way to get the verifier p@21: import urlparse, cgi p@21: query = urlparse.urlparse(response)[4] p@21: params = cgi.parse_qs(query, keep_blank_values=False) p@21: verifier = params['oauth_verifier'][0] p@21: print 'verifier: %s' % verifier p@21: pause() p@21: p@21: # get access token p@21: print '* Obtain an access token ...' p@21: pause() p@21: oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url) p@21: oauth_request.sign_request(signature_method_plaintext, consumer, token) p@21: print 'REQUEST (via headers)' p@21: print 'parameters: %s' % str(oauth_request.parameters) p@21: pause() p@21: token = client.fetch_access_token(oauth_request) p@21: print 'GOT' p@21: print 'key: %s' % str(token.key) p@21: print 'secret: %s' % str(token.secret) p@21: pause() p@21: p@21: # access some protected resources p@21: print '* Access protected resources ...' p@21: pause() p@21: parameters = {'file': 'vacation.jpg', 'size': 'original'} # resource specific params p@21: oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE_URL, parameters=parameters) p@21: oauth_request.sign_request(signature_method_hmac_sha1, consumer, token) p@21: print 'REQUEST (via post body)' p@21: print 'parameters: %s' % str(oauth_request.parameters) p@21: pause() p@21: params = client.access_resource(oauth_request) p@21: print 'GOT' p@21: print 'non-oauth parameters: %s' % params p@21: pause() p@21: p@21: def pause(): p@21: print '' p@21: time.sleep(1) p@21: p@21: if __name__ == '__main__': p@21: run_example() p@21: print 'Done.'