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