Skip to content

Instantly share code, notes, and snippets.

@swaroopch
Created February 3, 2012 06:48
Show Gist options
  • Save swaroopch/1728573 to your computer and use it in GitHub Desktop.
Save swaroopch/1728573 to your computer and use it in GitHub Desktop.
Two Legged OAuth API server (not working)
#!/usr/bin/env python
import oauth2 as oauth # pip install oauth2
parameters = {
'oauth_version' : '1.0',
'oauth_nonce' : oauth.generate_nonce(),
'oauth_timestamp' : oauth.generate_timestamp(),
}
token = oauth.Token(key='dummyuser', secret='dummypass')
consumer = oauth.Consumer(key='consumerkey', secret='consumerpass')
client = oauth.Client(consumer, token)
response = client.request('http://localhost:5000/secretagents')
print response
$ python server.py &
$ python client.py
Error: Invalid signature. Expected signature base string: GET&http%3A%2F%2Flocalhost%3A5000%2Fsecretagents&oauth_body_hash%3D2jmj7l5rSw0yVb%252FvlWAYkK%252FYBwk%253D%26oauth_body_hash%3D2jmj7l5rSw0yVb%252FvlWAYkK%252FYBwk%253D%26oauth_consumer_key%3Dconsumerkey%26oauth_consumer_key%3Dconsumerkey%26oauth_nonce%3D58247315%26oauth_nonce%3D58247315%26oauth_signature_method%3DHMAC-SHA1%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1328251508%26oauth_timestamp%3D1328251508%26oauth_token%3Ddummyuser%26oauth_token%3Ddummyuser%26oauth_version%3D1.0%26oauth_version%3D1.0
#!/usr/bin/env python
from flask import Flask, request
app = Flask(__name__)
import oauth2 as oauth # pip install oauth2
server = oauth.Server()
server.add_signature_method(oauth.SignatureMethod_HMAC_SHA1)
def token_from_key(username):
assert username == 'dummyuser'
return oauth.Token('dummyuser', 'dummypass')
def consumer_from_key(key):
assert key == 'consumerkey'
return oauth.Consumer('consumerkey', 'consumerpass')
@app.route('/secretagents')
def secret_agents():
oauth_request = oauth.Request.from_request(request.method, request.url,
parameters=(dict([(k, v) for (k, v) in request.values.iteritems()])) )
consumer = consumer_from_key(request.values.get('oauth_consumer_key'))
token = token_from_key(request.values.get('oauth_token'))
print server.verify_request(oauth_request, consumer, token)
return ''
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment