Skip to content
Browse files

Automatic token/secret-grabbing from a callback

  • Loading branch information...
1 parent 25ce646 commit 1c971eedad1830a3c4b61d6a501a7a3c76217032 @spicyj spicyj committed Feb 25, 2012
Showing with 47 additions and 15 deletions.
  1. +41 −11 examples/test_client/test.py
  2. +6 −4 examples/test_client/test_oauth_client.py
View
52 examples/test_client/test.py
@@ -1,3 +1,10 @@
+import cgi
+import os
+import readline
+import SocketServer
+import SimpleHTTPServer
+import sys
+
sys.path.insert(0, os.path.abspath('../../lib'))
from test_oauth_client import TestOAuthClient
@@ -13,23 +20,39 @@
REQUEST_TOKEN = None
ACCESS_TOKEN = None
-def get_request_token():
- global REQUEST_TOKEN
+def create_callback_server():
+ class CallbackHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
+ def do_GET(self):
+ global REQUEST_TOKEN
- client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
- client.start_fetch_request_token()
+ params = cgi.parse_qs(self.path.split('?', 1)[1], keep_blank_values=False)
+ REQUEST_TOKEN = OAuthToken(params['oauth_token'][0], params['oauth_token_secret'][0])
+ REQUEST_TOKEN.set_verifier(params['oauth_verifier'][0])
+
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/plain')
+ self.end_headers()
+ self.wfile.write('OAuth request token fetched; you can close this window.')
+
+ def log_request(self, code='-', size='-'):
+ pass
- print "After logging in and authorizing, input token key and secret..."
+ server = SocketServer.TCPServer(('127.0.0.1', 0), CallbackHandler)
+ return server
- request_token_key = raw_input("request token: ")
- request_token_secret = raw_input("request token secret: ")
+def get_request_token():
+ server = create_callback_server()
+
+ client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
+ client.start_fetch_request_token('http://127.0.0.1:%d/' % server.server_address[1])
- REQUEST_TOKEN = OAuthToken(request_token_key, request_token_secret)
+ server.handle_request()
+ # REQUEST_TOKEN has now been set
+ server.server_close()
def get_access_token():
global ACCESS_TOKEN
- print "Fetching access token..."
client = TestOAuthClient(SERVER_URL, CONSUMER_KEY, CONSUMER_SECRET)
ACCESS_TOKEN = client.fetch_access_token(REQUEST_TOKEN)
@@ -50,13 +73,17 @@ def run_tests():
global CONSUMER_KEY, CONSUMER_SECRET, SERVER_URL
CONSUMER_KEY = raw_input("consumer key (anyone): ") or "anyone"
CONSUMER_SECRET = raw_input("consumer secret (anyone): ") or "anyone"
- SERVER_URL = raw_input("server base url (http://local.kamenstestapp.appspot.com:8084): ") or "http://local.kamenstestapp.appspot.com:8084"
+ SERVER_URL = raw_input("server base url (http://www.khanacademy.org): ") or "http://www.khanacademy.org"
+
+ # It's a bit annoying for key/secret to be in readline history
+ readline.clear_history()
+ print
get_request_token()
if not REQUEST_TOKEN:
print "Did not get request token."
return
-
+
get_access_token()
if not ACCESS_TOKEN:
print "Did not get access token."
@@ -65,6 +92,9 @@ def run_tests():
while(True):
try:
get_api_resource()
+ except EOFError:
+ print
+ break
except Exception, e:
print "Error: %s" % e
View
10 examples/test_client/test_oauth_client.py
@@ -12,10 +12,11 @@ def __init__(self, server_url, consumer_key, consumer_secret):
self.server_url = server_url
self.consumer = OAuthConsumer(consumer_key, consumer_secret)
- def start_fetch_request_token(self):
+ def start_fetch_request_token(self, callback=None):
oauth_request = OAuthRequest.from_consumer_and_token(
self.consumer,
- http_url = "%s/api/auth/request_token" % self.server_url
+ callback=callback,
+ http_url="%s/api/auth/request_token" % self.server_url
)
oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None)
@@ -25,8 +26,9 @@ def fetch_access_token(self, request_token):
oauth_request = OAuthRequest.from_consumer_and_token(
self.consumer,
- token = request_token,
- http_url = "%s/api/auth/access_token" % self.server_url
+ token=request_token,
+ verifier=request_token.verifier,
+ http_url="%s/api/auth/access_token" % self.server_url
)
oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, request_token)

0 comments on commit 1c971ee

Please sign in to comment.
Something went wrong with that request. Please try again.