Skip to content

Commit

Permalink
Added support for really dumb OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
migurski committed Nov 28, 2010
1 parent c9038cf commit fa8711d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions canijust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from sys import stderr, exit
from optparse import OptionParser
from dwim import oauthdance

parser = OptionParser(usage="""%prog [options] <consumer key> <consumer secret>""")

defaults = {
'request_token_url': 'http://twitter.com/oauth/request_token',
'authorize_url': 'http://twitter.com/oauth/authorize',
'access_token_url': 'http://twitter.com/oauth/access_token'
}

parser.set_defaults(**defaults)

parser.add_option('-r', '--request-token-url', dest='request_token_url',
help='Default: %s' % defaults['request_token_url'])

parser.add_option('-a', '--authorize-url', dest='authorize_url',
help='Default: %s' % defaults['authorize_url'])

parser.add_option('-t', '--access-token-url', dest='access_token_url',
help='Default: %s' % defaults['access_token_url'])

if __name__ == '__main__':
options, args = parser.parse_args()

try:
key, secret = args
except ValueError:
print >> stderr, 'Need a key and secret, got: ' + repr(args)
exit(1)

oauthdance(key, secret, options.request_token_url, options.authorize_url, options.access_token_url)
36 changes: 36 additions & 0 deletions dwim.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,39 @@ def str2time(s):
dt -= dt.utcoffset()

return int(tuple2epoch(dt.timetuple()))

def oauthdance(consumer_key, consumer_secret, request_token_url='http://twitter.com/oauth/request_token', authorize_url='http://twitter.com/oauth/authorize', access_token_url='http://twitter.com/oauth/access_token'):
"""
"""
from oauth2 import Consumer, Client, Token
from urlparse import parse_qsl

con = Consumer(consumer_key, consumer_secret)
cli = Client(con)

res, bod = cli.request(request_token_url, 'GET')

assert res['status'] == '200', 'Expected status=200, got %s.' % res['status']

tok = dict(parse_qsl(bod))
tok = Token(tok['oauth_token'], tok['oauth_token_secret'])

print 'Visit this URL to get a PIN:'
print ' %s?oauth_token=%s' % (authorize_url, tok.key)

pin = raw_input('PIN: ').strip()

tok.set_verifier(pin)
cli = Client(con, tok)

res, bod = cli.request(access_token_url, 'GET')

assert res['status'] == '200', 'Expected status=200, got %s.' % res['status']

tok = dict(parse_qsl(bod))
tok = Token(tok['oauth_token'], tok['oauth_token_secret'])

print 'Your token key is: ', tok.key
print 'And your secret is:', tok.secret

return tok

0 comments on commit fa8711d

Please sign in to comment.