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