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