comparison Code/python_oauth2-master/example/client.py @ 21:e68dbee1f6db

Modified code New datasets Updated report
author Paulo Chiliguano <p.e.chiilguano@se14.qmul.ac.uk>
date Tue, 11 Aug 2015 10:50:36 +0100
parents
children
comparison
equal deleted inserted replaced
20:1dbd24575d44 21:e68dbee1f6db
1 """
2 The MIT License
3
4 Copyright (c) 2007 Leah Culver
5
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:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
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
22 THE SOFTWARE.
23
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.
27 """
28
29 import httplib
30 import time
31 import oauth.oauth as oauth
32
33 # settings for the local test consumer
34 SERVER = 'localhost'
35 PORT = 8080
36
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'
43
44 # key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore
45 CONSUMER_KEY = 'key'
46 CONSUMER_SECRET = 'secret'
47
48 # example client using httplib with headers
49 class SimpleOAuthClient(oauth.OAuthClient):
50
51 def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''):
52 self.server = server
53 self.port = port
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))
58
59 def fetch_request_token(self, oauth_request):
60 # via headers
61 # -> OAuthToken
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())
65
66 def fetch_access_token(self, oauth_request):
67 # via headers
68 # -> OAuthToken
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())
72
73 def authorize_token(self, oauth_request):
74 # via url
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()
79
80 def access_resource(self, oauth_request):
81 # via post body
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()
87
88 def run_example():
89
90 # setup
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()
96 pause()
97
98 # get request token
99 print '* Obtain a request token ...'
100 pause()
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)
105 pause()
106 token = client.fetch_request_token(oauth_request)
107 print 'GOT'
108 print 'key: %s' % str(token.key)
109 print 'secret: %s' % str(token.secret)
110 print 'callback confirmed? %s' % str(token.callback_confirmed)
111 pause()
112
113 print '* Authorize the request token ...'
114 pause()
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)
118 pause()
119 # this will actually occur only on some callback
120 response = client.authorize_token(oauth_request)
121 print 'GOT'
122 print response
123 # sad way to get the verifier
124 import urlparse, cgi
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
129 pause()
130
131 # get access token
132 print '* Obtain an access token ...'
133 pause()
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)
138 pause()
139 token = client.fetch_access_token(oauth_request)
140 print 'GOT'
141 print 'key: %s' % str(token.key)
142 print 'secret: %s' % str(token.secret)
143 pause()
144
145 # access some protected resources
146 print '* Access protected resources ...'
147 pause()
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)
153 pause()
154 params = client.access_resource(oauth_request)
155 print 'GOT'
156 print 'non-oauth parameters: %s' % params
157 pause()
158
159 def pause():
160 print ''
161 time.sleep(1)
162
163 if __name__ == '__main__':
164 run_example()
165 print 'Done.'