]>
Commit | Line | Data |
---|---|---|
ea5231b2 MV |
1 | from __future__ import print_function |
2 | ||
ddeba164 MV |
3 | import webbrowser |
4 | import time | |
1b31d642 | 5 | |
f7e63802 MV |
6 | from .api import Twitter |
7 | from .oauth import OAuth, write_token_file | |
1b31d642 | 8 | |
ea5231b2 MV |
9 | try: |
10 | _input = raw_input | |
11 | except NameError: | |
12 | _input = input | |
13 | ||
14 | ||
15 | ||
1b31d642 MV |
16 | def oauth_dance(app_name, consumer_key, consumer_secret, token_filename=None): |
17 | """ | |
18 | Perform the OAuth dance with some command-line prompts. Return the | |
19 | oauth_token and oauth_token_secret. | |
20 | ||
21 | Provide the name of your app in `app_name`, your consumer_key, and | |
22 | consumer_secret. This function will open a web browser to let the | |
a6a7f763 | 23 | user allow your app to access their Twitter account. PIN |
1b31d642 MV |
24 | authentication is used. |
25 | ||
26 | If a token_filename is given, the oauth tokens will be written to | |
27 | the file. | |
28 | """ | |
652c5402 | 29 | print("Hi there! We're gonna get you all set up to use %s." % app_name) |
1b31d642 MV |
30 | twitter = Twitter( |
31 | auth=OAuth('', '', consumer_key, consumer_secret), | |
ea5231b2 | 32 | format='', api_version=None) |
1b31d642 MV |
33 | oauth_token, oauth_token_secret = parse_oauth_tokens( |
34 | twitter.oauth.request_token()) | |
f7e63802 | 35 | print(""" |
1b31d642 MV |
36 | In the web browser window that opens please choose to Allow |
37 | access. Copy the PIN number that appears on the next page and paste or | |
38 | type it here: | |
f7e63802 | 39 | """) |
f0d38884 | 40 | oauth_url = ('https://api.twitter.com/oauth/authorize?oauth_token=' + |
25feb118 | 41 | oauth_token) |
f7e63802 | 42 | print("Opening: %s\n" % oauth_url) |
dda98f55 | 43 | |
25feb118 MV |
44 | try: |
45 | r = webbrowser.open(oauth_url) | |
46 | time.sleep(2) # Sometimes the last command can print some | |
47 | # crap. Wait a bit so it doesn't mess up the next | |
48 | # prompt. | |
49 | if not r: | |
50 | raise Exception() | |
51 | except: | |
f7e63802 | 52 | print(""" |
25feb118 MV |
53 | Uh, I couldn't open a browser on your computer. Please go here to get |
54 | your PIN: | |
55 | ||
f7e63802 | 56 | """ + oauth_url) |
ea5231b2 | 57 | oauth_verifier = _input("Please enter the PIN: ").strip() |
1b31d642 MV |
58 | twitter = Twitter( |
59 | auth=OAuth( | |
ddeba164 | 60 | oauth_token, oauth_token_secret, consumer_key, consumer_secret), |
ea5231b2 | 61 | format='', api_version=None) |
1b31d642 MV |
62 | oauth_token, oauth_token_secret = parse_oauth_tokens( |
63 | twitter.oauth.access_token(oauth_verifier=oauth_verifier)) | |
64 | if token_filename: | |
65 | write_token_file( | |
66 | token_filename, oauth_token, oauth_token_secret) | |
f7e63802 MV |
67 | print() |
68 | print("That's it! Your authorization keys have been written to %s." % ( | |
69 | token_filename)) | |
ddeba164 MV |
70 | return oauth_token, oauth_token_secret |
71 | ||
72 | def parse_oauth_tokens(result): | |
73 | for r in result.split('&'): | |
74 | k, v = r.split('=') | |
75 | if k == 'oauth_token': | |
76 | oauth_token = v | |
77 | elif k == 'oauth_token_secret': | |
78 | oauth_token_secret = v | |
1b31d642 | 79 | return oauth_token, oauth_token_secret |