]> jfr.im git - z_archive/twitter.git/blob - twitter/oauth_dance.py
Update .gitignore
[z_archive/twitter.git] / twitter / oauth_dance.py
1
2
3 from api import Twitter
4 from oauth import OAuth, write_token_file
5
6 def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None):
7 """
8 Perform the OAuth dance with some command-line prompts. Return the
9 oauth_token and oauth_token_secret.
10
11 Provide the name of your app in `app_name`, your consumer_key, and
12 consumer_secret. This function will open a web browser to let the
13 user Allow your app to access their Twitter account. PIN
14 authentication is used.
15
16 If a token_filename is given, the oauth tokens will be written to
17 the file.
18 """
19 print ("Hi there! We're gonna get you all set up to use %s." % app_name)
20 twitter = Twitter(
21 auth=OAuth('', '', consumer_key, consumer_secret),
22 format='')
23 oauth_token, oauth_token_secret = parse_oauth_tokens(
24 twitter.oauth.request_token())
25 print """
26 In the web browser window that opens please choose to Allow
27 access. Copy the PIN number that appears on the next page and paste or
28 type it here:
29 """
30 webbrowser.open(
31 'http://api.twitter.com/oauth/authorize?oauth_token=' +
32 oauth_token)
33 time.sleep(2) # Sometimes the last command can print some
34 # crap. Wait a bit so it doesn't mess up the next
35 # prompt.
36 oauth_verifier = raw_input("Please type the PIN: ").strip()
37 twitter = Twitter(
38 auth=OAuth(
39 oauth_token, oauth_token_secret, CONSUMER_KEY, CONSUMER_SECRET),
40 format='')
41 oauth_token, oauth_token_secret = parse_oauth_tokens(
42 twitter.oauth.access_token(oauth_verifier=oauth_verifier))
43 if token_filename:
44 write_token_file(
45 token_filename, oauth_token, oauth_token_secret)
46 print
47 print "That's it! Your authorization keys have been written to %s." % (
48 options['oauth_filename'])
49 return oauth_token, oauth_token_secret