]>
jfr.im git - z_archive/twitter.git/blob - twitter/oauth_dance.py
a013c422f66992642fbb2fdcac52d58a31160fd8
2 from __future__
import print_function
7 from .api
import Twitter
8 from .oauth
import OAuth
, write_token_file
17 def oauth_dance(app_name
, consumer_key
, consumer_secret
, token_filename
=None):
19 Perform the OAuth dance with some command-line prompts. Return the
20 oauth_token and oauth_token_secret.
22 Provide the name of your app in `app_name`, your consumer_key, and
23 consumer_secret. This function will open a web browser to let the
24 user Allow your app to access their Twitter account. PIN
25 authentication is used.
27 If a token_filename is given, the oauth tokens will be written to
30 print("Hi there! We're gonna get you all set up to use %s." % app_name
)
32 auth
=OAuth('', '', consumer_key
, consumer_secret
),
33 format
='', api_version
=None)
34 oauth_token
, oauth_token_secret
= parse_oauth_tokens(
35 twitter
.oauth
.request_token())
37 In the web browser window that opens please choose to Allow
38 access. Copy the PIN number that appears on the next page and paste or
41 oauth_url
= ('http://api.twitter.com/oauth/authorize?oauth_token=' +
43 print("Opening: %s\n" % oauth_url
)
46 r
= webbrowser
.open(oauth_url
)
47 time
.sleep(2) # Sometimes the last command can print some
48 # crap. Wait a bit so it doesn't mess up the next
54 Uh, I couldn't open a browser on your computer. Please go here to get
58 oauth_verifier
= _input("Please enter the PIN: ").strip()
61 oauth_token
, oauth_token_secret
, consumer_key
, consumer_secret
),
62 format
='', api_version
=None)
63 oauth_token
, oauth_token_secret
= parse_oauth_tokens(
64 twitter
.oauth
.access_token(oauth_verifier
=oauth_verifier
))
67 token_filename
, oauth_token
, oauth_token_secret
)
69 print("That's it! Your authorization keys have been written to %s." % (
71 return oauth_token
, oauth_token_secret
73 def parse_oauth_tokens(result
):
74 for r
in result
.split('&'):
76 if k
== 'oauth_token':
78 elif k
== 'oauth_token_secret':
79 oauth_token_secret
= v
80 return oauth_token
, oauth_token_secret