def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): """ Perform the OAuth dance with some command-line prompts. Return the oauth_token and oauth_token_secret. Provide the name of your app in `app_name`, your consumer_key, and consumer_secret. This function will open a web browser to let the user Allow your app to access their Twitter account. PIN authentication is used. 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) twitter = Twitter( auth=OAuth('', '', consumer_key, consumer_secret), format='', api_version=None) oauth_token, oauth_token_secret = parse_oauth_tokens( twitter.oauth.request_token()) 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: """) oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token) print("Opening: %s\n" % oauth_url) try: r = webbrowser.open(oauth_url) # Sometimes the last command can print some crap. Wait a bit so it # doesn't mess up the next prompt. time.sleep(2) 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), format='', api_version=None) oauth_token, oauth_token_secret = parse_oauth_tokens( twitter.oauth.access_token(oauth_verifier=oauth_verifier)) if token_filename: OAuth.write_token_file( token_filename, oauth_token, oauth_token_secret) print() print("That's it! Your authorization keys have been written to %s." % ( token_filename)) return oauth_token, oauth_token_secret
def main(args=sys.argv[1:]): arg_options = {} try: parse_args(args, arg_options) except GetoptError as e: print("I can't do that, %s." % (e), file=sys.stderr) print(file=sys.stderr) raise SystemExit(1) config_path = os.path.expanduser( arg_options.get('config_filename') or OPTIONS.get('config_filename')) config_options = loadConfig(config_path) # Apply the various options in order, the most important applied last. # Defaults first, then what's read from config file, then command-line # arguments. options = dict(OPTIONS) for d in config_options, arg_options: for k, v in list(d.items()): if v: options[k] = v if options['refresh'] and options['action'] not in ( 'friends', 'public', 'replies'): print("You can only refresh the friends, public, or replies actions.", file=sys.stderr) print("Use 'twitter -h' for help.", file=sys.stderr) return 1 oauth_filename = os.path.expanduser(options['oauth_filename']) if (options['action'] == 'authorize' or not os.path.exists(oauth_filename)): oauth_dance( "the Command-Line Tool", CONSUMER_KEY, CONSUMER_SECRET, options['oauth_filename']) oauth_token, oauth_token_secret = OAuth.read_token_file(oauth_filename) twitter = Twitter( auth=OAuth( oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET), secure=options['secure'], api_version='1', domain='api.twitter.com') try: Action()(twitter, options) except NoSuchActionError as e: print(e, file=sys.stderr) raise SystemExit(1) except APIHTTPError as e: print(str(e), file=sys.stderr) print("Use 'twitter -h' for help.", file=sys.stderr) raise SystemExit(1)