]>
Commit | Line | Data |
---|---|---|
1b31d642 | 1 | |
ddeba164 MV |
2 | import webbrowser |
3 | import time | |
1b31d642 | 4 | |
f7e63802 MV |
5 | from .api import Twitter |
6 | from .oauth import OAuth, write_token_file | |
1b31d642 MV |
7 | |
8 | def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): | |
9 | """ | |
10 | Perform the OAuth dance with some command-line prompts. Return the | |
11 | oauth_token and oauth_token_secret. | |
12 | ||
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. | |
17 | ||
18 | If a token_filename is given, the oauth tokens will be written to | |
19 | the file. | |
20 | """ | |
f7e63802 | 21 | print(("Hi there! We're gonna get you all set up to use %s." % app_name)) |
1b31d642 MV |
22 | twitter = Twitter( |
23 | auth=OAuth('', '', consumer_key, consumer_secret), | |
24 | format='') | |
25 | oauth_token, oauth_token_secret = parse_oauth_tokens( | |
26 | twitter.oauth.request_token()) | |
f7e63802 | 27 | print(""" |
1b31d642 MV |
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 | |
30 | type it here: | |
f7e63802 | 31 | """) |
25feb118 MV |
32 | oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + |
33 | oauth_token) | |
f7e63802 | 34 | print("Opening: %s\n" % oauth_url) |
dda98f55 | 35 | |
25feb118 MV |
36 | try: |
37 | r = webbrowser.open(oauth_url) | |
38 | time.sleep(2) # Sometimes the last command can print some | |
39 | # crap. Wait a bit so it doesn't mess up the next | |
40 | # prompt. | |
41 | if not r: | |
42 | raise Exception() | |
43 | except: | |
f7e63802 | 44 | print(""" |
25feb118 MV |
45 | Uh, I couldn't open a browser on your computer. Please go here to get |
46 | your PIN: | |
47 | ||
f7e63802 MV |
48 | """ + oauth_url) |
49 | oauth_verifier = input("Please enter the PIN: ").strip() | |
1b31d642 MV |
50 | twitter = Twitter( |
51 | auth=OAuth( | |
ddeba164 | 52 | oauth_token, oauth_token_secret, consumer_key, consumer_secret), |
1b31d642 MV |
53 | format='') |
54 | oauth_token, oauth_token_secret = parse_oauth_tokens( | |
55 | twitter.oauth.access_token(oauth_verifier=oauth_verifier)) | |
56 | if token_filename: | |
57 | write_token_file( | |
58 | token_filename, oauth_token, oauth_token_secret) | |
f7e63802 MV |
59 | print() |
60 | print("That's it! Your authorization keys have been written to %s." % ( | |
61 | token_filename)) | |
ddeba164 MV |
62 | return oauth_token, oauth_token_secret |
63 | ||
64 | def parse_oauth_tokens(result): | |
65 | for r in result.split('&'): | |
66 | k, v = r.split('=') | |
67 | if k == 'oauth_token': | |
68 | oauth_token = v | |
69 | elif k == 'oauth_token_secret': | |
70 | oauth_token_secret = v | |
1b31d642 | 71 | return oauth_token, oauth_token_secret |