Permalink
Switch branches/tags
pre-1.0a hudson-python-oauth2-209 hudson-python-oauth2-208 hudson-python-oauth2-207 hudson-python-oauth2-206 hudson-python-oauth2-205 hudson-python-oauth2-204 hudson-python-oauth2-203 hudson-python-oauth2-202 hudson-python-oauth2-201 hudson-python-oauth2-200 hudson-python-oauth2-199 hudson-python-oauth2-170 hudson-python-oauth2-169 hudson-python-oauth2-169-SUCCESS hudson-python-oauth2-168 hudson-python-oauth2-168-SUCCESS hudson-python-oauth2-167 hudson-python-oauth2-167-SUCCESS hudson-python-oauth2-166 hudson-python-oauth2-166-SUCCESS hudson-python-oauth2-165 hudson-python-oauth2-165-SUCCESS hudson-python-oauth2-164 hudson-python-oauth2-164-SUCCESS hudson-python-oauth2-163 hudson-python-oauth2-163-SUCCESS hudson-python-oauth2-162 hudson-python-oauth2-162-SUCCESS hudson-python-oauth2-161 hudson-python-oauth2-161-SUCCESS hudson-python-oauth2-160 hudson-python-oauth2-160-SUCCESS hudson-python-oauth2-159 hudson-python-oauth2-159-SUCCESS hudson-python-oauth2-158 hudson-python-oauth2-158-SUCCESS hudson-python-oauth2-157 hudson-python-oauth2-156 hudson-python-oauth2-155 hudson-python-oauth2-155-SUCCESS hudson-python-oauth2-154 hudson-python-oauth2-154-SUCCESS hudson-python-oauth2-153 hudson-python-oauth2-153-SUCCESS hudson-python-oauth2-152 hudson-python-oauth2-152-SUCCESS hudson-python-oauth2-151 hudson-python-oauth2-151-SUCCESS hudson-python-oauth2-150 hudson-python-oauth2-150-SUCCESS hudson-python-oauth2-149 hudson-python-oauth2-149-SUCCESS hudson-python-oauth2-148 hudson-python-oauth2-148-SUCCESS hudson-python-oauth2-147 hudson-python-oauth2-147-SUCCESS hudson-python-oauth2-146 hudson-python-oauth2-146-SUCCESS hudson-python-oauth2-145 hudson-python-oauth2-145-SUCCESS hudson-python-oauth2-144 hudson-python-oauth2-144-SUCCESS hudson-python-oauth2-143 hudson-python-oauth2-143-SUCCESS hudson-python-oauth2-142 hudson-python-oauth2-141 hudson-python-oauth2-140 hudson-python-oauth2-139 hudson-python-oauth2-138 hudson-python-oauth2-137 hudson-python-oauth2-136 hudson-python-oauth2-135 hudson-python-oauth2-134 hudson-python-oauth2-134-SUCCESS hudson-python-oauth2-133 hudson-python-oauth2-133-SUCCESS hudson-python-oauth2-132 hudson-python-oauth2-132-SUCCESS hudson-python-oauth2-131 hudson-python-oauth2-131-SUCCESS hudson-python-oauth2-130 hudson-python-oauth2-130-SUCCESS hudson-python-oauth2-129 hudson-python-oauth2-129-SUCCESS hudson-python-oauth2-128 hudson-python-oauth2-128-SUCCESS hudson-python-oauth2-127 hudson-python-oauth2-127-SUCCESS debian/1.5.209 debian/1.5.208 debian/1.5.207 debian/1.5.206 debian/1.5.205 debian/1.5.204 debian/1.5.203 debian/1.5.202 debian/1.5.201 debian/1.5.200 debian/1.5.199
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (142 sloc) 6.54 KB
"""
The MIT License
Copyright (c) 2007 Leah Culver
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Example consumer. This is not recommended for production.
Instead, you'll want to create your own subclass of OAuthClient
or find one that works with your web framework.
"""
import httplib
import time
import oauth.oauth as oauth
# settings for the local test consumer
SERVER = 'localhost'
PORT = 8080
# fake urls for the test server (matches ones in server.py)
REQUEST_TOKEN_URL = 'https://photos.example.net/request_token'
ACCESS_TOKEN_URL = 'https://photos.example.net/access_token'
AUTHORIZATION_URL = 'https://photos.example.net/authorize'
CALLBACK_URL = 'http://printer.example.com/request_token_ready'
RESOURCE_URL = 'http://photos.example.net/photos'
# key and secret granted by the service provider for this consumer application - same as the MockOAuthDataStore
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
# example client using httplib with headers
class SimpleOAuthClient(oauth.OAuthClient):
def __init__(self, server, port=httplib.HTTP_PORT, request_token_url='', access_token_url='', authorization_url=''):
self.server = server
self.port = port
self.request_token_url = request_token_url
self.access_token_url = access_token_url
self.authorization_url = authorization_url
self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port))
def fetch_request_token(self, oauth_request):
# via headers
# -> OAuthToken
self.connection.request(oauth_request.http_method, self.request_token_url, headers=oauth_request.to_header())
response = self.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())
def fetch_access_token(self, oauth_request):
# via headers
# -> OAuthToken
self.connection.request(oauth_request.http_method, self.access_token_url, headers=oauth_request.to_header())
response = self.connection.getresponse()
return oauth.OAuthToken.from_string(response.read())
def authorize_token(self, oauth_request):
# via url
# -> typically just some okay response
self.connection.request(oauth_request.http_method, oauth_request.to_url())
response = self.connection.getresponse()
return response.read()
def access_resource(self, oauth_request):
# via post body
# -> some protected resources
headers = {'Content-Type' :'application/x-www-form-urlencoded'}
self.connection.request('POST', RESOURCE_URL, body=oauth_request.to_postdata(), headers=headers)
response = self.connection.getresponse()
return response.read()
def run_example():
# setup
print '** OAuth Python Library Example **'
client = SimpleOAuthClient(SERVER, PORT, REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL)
consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
signature_method_plaintext = oauth.OAuthSignatureMethod_PLAINTEXT()
signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1()
pause()
# get request token
print '* Obtain a request token ...'
pause()
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, callback=CALLBACK_URL, http_url=client.request_token_url)
oauth_request.sign_request(signature_method_plaintext, consumer, None)
print 'REQUEST (via headers)'
print 'parameters: %s' % str(oauth_request.parameters)
pause()
token = client.fetch_request_token(oauth_request)
print 'GOT'
print 'key: %s' % str(token.key)
print 'secret: %s' % str(token.secret)
print 'callback confirmed? %s' % str(token.callback_confirmed)
pause()
print '* Authorize the request token ...'
pause()
oauth_request = oauth.OAuthRequest.from_token_and_callback(token=token, http_url=client.authorization_url)
print 'REQUEST (via url query string)'
print 'parameters: %s' % str(oauth_request.parameters)
pause()
# this will actually occur only on some callback
response = client.authorize_token(oauth_request)
print 'GOT'
print response
# sad way to get the verifier
import urlparse, cgi
query = urlparse.urlparse(response)[4]
params = cgi.parse_qs(query, keep_blank_values=False)
verifier = params['oauth_verifier'][0]
print 'verifier: %s' % verifier
pause()
# get access token
print '* Obtain an access token ...'
pause()
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, verifier=verifier, http_url=client.access_token_url)
oauth_request.sign_request(signature_method_plaintext, consumer, token)
print 'REQUEST (via headers)'
print 'parameters: %s' % str(oauth_request.parameters)
pause()
token = client.fetch_access_token(oauth_request)
print 'GOT'
print 'key: %s' % str(token.key)
print 'secret: %s' % str(token.secret)
pause()
# access some protected resources
print '* Access protected resources ...'
pause()
parameters = {'file': 'vacation.jpg', 'size': 'original'} # resource specific params
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_method='POST', http_url=RESOURCE_URL, parameters=parameters)
oauth_request.sign_request(signature_method_hmac_sha1, consumer, token)
print 'REQUEST (via post body)'
print 'parameters: %s' % str(oauth_request.parameters)
pause()
params = client.access_resource(oauth_request)
print 'GOT'
print 'non-oauth parameters: %s' % params
pause()
def pause():
print ''
time.sleep(1)
if __name__ == '__main__':
run_example()
print 'Done.'