X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/1b31d642c54e09dc8f8a1ca49c934d6990f2ab33..79d6cca767a245eb3a115f3698a9b388b36ab987:/twitter/oauth_dance.py diff --git a/twitter/oauth_dance.py b/twitter/oauth_dance.py index cffa624..dace720 100644 --- a/twitter/oauth_dance.py +++ b/twitter/oauth_dance.py @@ -1,7 +1,9 @@ +import webbrowser +import time -from api import Twitter -from oauth import OAuth, write_token_file +from .api import Twitter +from .oauth import OAuth, write_token_file def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): """ @@ -16,34 +18,54 @@ def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): If a token_filename is given, the oauth tokens will be written to the file. """ - print ("Hi there! We're gonna get you all set up to use %s." % app_name) + print(("Hi there! We're gonna get you all set up to use %s." % app_name)) twitter = Twitter( auth=OAuth('', '', consumer_key, consumer_secret), format='') oauth_token, oauth_token_secret = parse_oauth_tokens( twitter.oauth.request_token()) - print """ + print(""" In the web browser window that opens please choose to Allow access. Copy the PIN number that appears on the next page and paste or type it here: -""" - webbrowser.open( - 'http://api.twitter.com/oauth/authorize?oauth_token=' + - oauth_token) - time.sleep(2) # Sometimes the last command can print some - # crap. Wait a bit so it doesn't mess up the next - # prompt. - oauth_verifier = raw_input("Please type the PIN: ").strip() +""") + oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + + oauth_token) + print("Opening: %s\n" % oauth_url) + + try: + r = webbrowser.open(oauth_url) + time.sleep(2) # Sometimes the last command can print some + # crap. Wait a bit so it doesn't mess up the next + # prompt. + if not r: + raise Exception() + except: + print(""" +Uh, I couldn't open a browser on your computer. Please go here to get +your PIN: + +""" + oauth_url) + oauth_verifier = input("Please enter the PIN: ").strip() twitter = Twitter( auth=OAuth( - oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET), + oauth_token, oauth_token_secret, consumer_key, consumer_secret), format='') oauth_token, oauth_token_secret = parse_oauth_tokens( twitter.oauth.access_token(oauth_verifier=oauth_verifier)) if token_filename: write_token_file( token_filename, oauth_token, oauth_token_secret) - print - print "That's it! Your authorization keys have been written to %s." % ( - options['oauth_filename']) + print() + print("That's it! Your authorization keys have been written to %s." % ( + token_filename)) + return oauth_token, oauth_token_secret + +def parse_oauth_tokens(result): + for r in result.split('&'): + k, v = r.split('=') + if k == 'oauth_token': + oauth_token = v + elif k == 'oauth_token_secret': + oauth_token_secret = v return oauth_token, oauth_token_secret