|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
17 | | -# parse_qsl moved to urlparse module in v2.6 |
18 | | -try: |
19 | | - from urlparse import parse_qsl |
20 | | -except: |
21 | | - from cgi import parse_qsl |
22 | | - |
23 | 17 | import webbrowser |
24 | | -import oauth2 as oauth |
| 18 | +from requests_oauthlib import OAuth1Session |
25 | 19 |
|
26 | 20 | REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token' |
27 | 21 | ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token' |
|
31 | 25 |
|
32 | 26 | def get_access_token(consumer_key, consumer_secret): |
33 | 27 |
|
34 | | - signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() |
35 | | - oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret) |
36 | | - oauth_client = oauth.Client(oauth_consumer) |
| 28 | + oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret) |
37 | 29 |
|
38 | 30 | print 'Requesting temp token from Twitter' |
39 | 31 |
|
40 | | - resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'POST', body="oauth_callback=oob") |
41 | | - |
42 | | - if resp['status'] != '200': |
43 | | - print 'Invalid respond from Twitter requesting temp token: %s' % resp['status'] |
44 | | - else: |
45 | | - request_token = dict(parse_qsl(content)) |
46 | | - url = '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token']) |
47 | | - |
48 | | - print '' |
49 | | - print 'I will try to start a browser to visit the following Twitter page' |
50 | | - print 'if a browser will not start, copy the URL to your browser' |
51 | | - print 'and retrieve the pincode to be used' |
52 | | - print 'in the next step to obtaining an Authentication Token:' |
53 | | - print '' |
54 | | - print url |
55 | | - print '' |
56 | | - |
57 | | - webbrowser.open(url) |
58 | | - pincode = raw_input('Pincode? ') |
59 | | - |
60 | | - token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) |
61 | | - token.set_verifier(pincode) |
62 | | - |
63 | | - print '' |
64 | | - print 'Generating and signing request for an access token' |
65 | | - print '' |
66 | | - |
67 | | - oauth_client = oauth.Client(oauth_consumer, token) |
68 | | - resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_callback=oob&oauth_verifier=%s' % pincode) |
69 | | - access_token = dict(parse_qsl(content)) |
70 | | - |
71 | | - if resp['status'] != '200': |
72 | | - print 'The request for a Token did not succeed: %s' % resp['status'] |
73 | | - print access_token |
74 | | - else: |
75 | | - print 'Your Twitter Access Token key: %s' % access_token['oauth_token'] |
76 | | - print ' Access Token secret: %s' % access_token['oauth_token_secret'] |
77 | | - print '' |
| 32 | + try: |
| 33 | + resp = oauth_client.fetch_request_token(REQUEST_TOKEN_URL) |
| 34 | + except ValueError, e: |
| 35 | + print 'Invalid respond from Twitter requesting temp token: %s' % e |
| 36 | + return |
| 37 | + url = oauth_client.authorization_url(AUTHORIZATION_URL) |
| 38 | + |
| 39 | + print '' |
| 40 | + print 'I will try to start a browser to visit the following Twitter page' |
| 41 | + print 'if a browser will not start, copy the URL to your browser' |
| 42 | + print 'and retrieve the pincode to be used' |
| 43 | + print 'in the next step to obtaining an Authentication Token:' |
| 44 | + print '' |
| 45 | + print url |
| 46 | + print '' |
| 47 | + |
| 48 | + webbrowser.open(url) |
| 49 | + pincode = raw_input('Pincode? ') |
| 50 | + |
| 51 | + |
| 52 | + print '' |
| 53 | + print 'Generating and signing request for an access token' |
| 54 | + print '' |
| 55 | + |
| 56 | + oauth_client = OAuth1Session(consumer_key, client_secret=consumer_secret, |
| 57 | + resource_owner_key=resp.get('oauth_token'), |
| 58 | + resource_owner_secret=resp.get('oauth_token_secret'), |
| 59 | + verifier=pincode |
| 60 | + ) |
| 61 | + try: |
| 62 | + resp = oauth_client.fetch_access_token(ACCESS_TOKEN_URL) |
| 63 | + except ValueError, e: |
| 64 | + print 'Invalid respond from Twitter requesting access token: %s' % e |
| 65 | + return |
| 66 | + |
| 67 | + print 'Your Twitter Access Token key: %s' % resp.get('oauth_token') |
| 68 | + print ' Access Token secret: %s' % resp.get('oauth_token_secret') |
| 69 | + print '' |
78 | 70 |
|
79 | 71 |
|
80 | 72 | def main(): |
|
0 commit comments