X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/ea5231b2fe0f40d87c9c7991b949c39c7cddd7e3..d0fa710a30df6236c7ec0421ae93242345d8dbf4:/twitter/oauth_dance.py diff --git a/twitter/oauth_dance.py b/twitter/oauth_dance.py index d6e93e3..43b88d0 100644 --- a/twitter/oauth_dance.py +++ b/twitter/oauth_dance.py @@ -1,11 +1,11 @@ - from __future__ import print_function import webbrowser import time -from .api import Twitter +from .api import Twitter, json from .oauth import OAuth, write_token_file +from .oauth2 import OAuth2, write_bearer_token_file try: _input = raw_input @@ -13,6 +13,22 @@ except NameError: _input = input +def oauth2_dance(consumer_key, consumer_secret, token_filename=None): + """ + Perform the OAuth2 dance to transform a consumer key and secret into a + bearer token. + + If a token_filename is given, the bearer token will be written to + the file. + """ + twitter = Twitter( + auth=OAuth2(consumer_key=consumer_key, consumer_secret=consumer_secret), + format="", + api_version="") + token = json.loads(twitter.oauth2.token(grant_type="client_credentials"))["access_token"] + if token_filename: + write_bearer_token_file(token) + return token def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): """ @@ -21,24 +37,24 @@ def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): 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 + 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)) + 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()) + twitter.oauth.request_token(oauth_callback="oob")) 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_url = ('https://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token) print("Opening: %s\n" % oauth_url)