4 Copyright (c) 2007 Leah Culver
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 Example consumer. This is not recommended for production.
25 Instead, you'll want to create your own subclass of OAuthClient
26 or find one that works with your web framework.
31 import oauth.oauth as oauth
33 # settings for the local test consumer
37 # fake urls for the test server (matches ones in server.py)
38 REQUEST_TOKEN_URL = 'https://photos.example.net/request_token'
39 ACCESS_TOKEN_URL = 'https://photos.example.net/access_token'
40 AUTHORIZATION_URL = 'https://photos.example.net/authorize'
41 CALLBACK_URL = 'http://printer.example.com/request_token_ready'
42 RESOURCE_URL = 'http://photos.example.net/photos'
44 # key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore
46 CONSUMER_SECRET = 'secret'
48 # example client using httplib with headers
49 class SimpleOAuthClient(oauth.OAuthClient):
51 def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''):
54 self.request_token_url = request_token_url
55 self.access_token_url = access_token_url
56 self.authorization_url = authorization_url
57 self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port))
59 def fetch_request_token(self, oauth_request):
62 self.connection.request(oauth_request.http_method, self.request_token_url, headers=oauth_request.to_header())
63 response = self.connection.getresponse()
64 return oauth.OAuthToken.from_string(response.read())
66 def fetch_access_token(self, oauth_request):
69 self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header())
70 response = self.connection.getresponse()
71 return oauth.OAuthToken.from_string(response.read())
73 def authorize_token(self, oauth_request):
75 # -> typically just some okay response
76 self.connection.request(oauth_request.http_method, oauth_request.to_url())
77 response = self.connection.getresponse()
78 return response.read()
80 def access_resource(self, oauth_request):
82 # -> some protected resources
83 headers = {'Content-Type' :'application/x-www-form-urlencoded'}
84 self.connection.request('POST', RESOURCE_URL, body=oauth_request.to_postdata(), headers=headers)
85 response = self.connection.getresponse()
86 return response.read()
91 print '** OAuth Python Library Example **'
92 client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
93 consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
94 signature_method_plaintext = oauth.OAuthSignatureMethod_PLAINTEXT()
95 signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
99 print '* Obtain a request token ...'
101 oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, callback=CALLBACK_URL, http_url=client.request_token_url)
102 oauth_request.sign_request(signature_method_plaintext, consumer, None)
103 print 'REQUEST (via headers)'
104 print 'parameters: %s' % str(oauth_request.parameters)
106 token = client.fetch_request_token(oauth_request)
108 print 'key: %s' % str(token.key)
109 print 'secret: %s' % str(token.secret)
110 print 'callback confirmed? %s' % str(token.callback_confirmed)
113 print '* Authorize the request token ...'
115 oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url)
116 print 'REQUEST (via url query string)'
117 print 'parameters: %s' % str(oauth_request.parameters)
119 # this will actually occur only on some callback
120 response = client.authorize_token(oauth_request)
123 # sad way to get the verifier
125 query = urlparse.urlparse(response)[4]
126 params = cgi.parse_qs(query, keep_blank_values=False)
127 verifier = params['oauth_verifier'][0]
128 print 'verifier: %s' % verifier
132 print '* Obtain an access token ...'
134 oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url)
135 oauth_request.sign_request(signature_method_plaintext, consumer, token)
136 print 'REQUEST (via headers)'
137 print 'parameters: %s' % str(oauth_request.parameters)
139 token = client.fetch_access_token(oauth_request)
141 print 'key: %s' % str(token.key)
142 print 'secret: %s' % str(token.secret)
145 # access some protected resources
146 print '* Access protected resources ...'
148 parameters = {'file': 'vacation.jpg', 'size': 'original'} # resource specific params
149 oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE_URL, parameters=parameters)
150 oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
151 print 'REQUEST (via post body)'
152 print 'parameters: %s' % str(oauth_request.parameters)
154 params = client.access_resource(oauth_request)
156 print 'non-oauth parameters: %s' % params
163 if __name__ == '__main__':