]>
jfr.im git - z_archive/twitter.git/blob - twitter/oauth_dance.py
dcbcc6ab5f40a7dd6b84c09a6cd151c7134de6ce
5 from api
import Twitter
6 from oauth
import OAuth
, write_token_file
8 def oauth_dance(app_name
, consumer_key
, consumer_secret
, token_filename
=None):
10 Perform the OAuth dance with some command-line prompts. Return the
11 oauth_token and oauth_token_secret.
13 Provide the name of your app in `app_name`, your consumer_key, and
14 consumer_secret. This function will open a web browser to let the
15 user Allow your app to access their Twitter account. PIN
16 authentication is used.
18 If a token_filename is given, the oauth tokens will be written to
21 print ("Hi there! We're gonna get you all set up to use %s." % app_name
)
23 auth
=OAuth('', '', consumer_key
, consumer_secret
),
25 oauth_token
, oauth_token_secret
= parse_oauth_tokens(
26 twitter
.oauth
.request_token())
28 In the web browser window that opens please choose to Allow
29 access. Copy the PIN number that appears on the next page and paste or
33 'http://api.twitter.com/oauth/authorize?oauth_token=' +
35 time
.sleep(2) # Sometimes the last command can print some
36 # crap. Wait a bit so it doesn't mess up the next
38 oauth_verifier
= raw_input("Please type the PIN: ").strip()
41 oauth_token
, oauth_token_secret
, consumer_key
, consumer_secret
),
43 oauth_token
, oauth_token_secret
= parse_oauth_tokens(
44 twitter
.oauth
.access_token(oauth_verifier
=oauth_verifier
))
47 token_filename
, oauth_token
, oauth_token_secret
)
49 print "That's it! Your authorization keys have been written to %s." % (
51 return oauth_token
, oauth_token_secret
53 def parse_oauth_tokens(result
):
54 for r
in result
.split('&'):
56 if k
== 'oauth_token':
58 elif k
== 'oauth_token_secret':
59 oauth_token_secret
= v
60 return oauth_token
, oauth_token_secret