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