1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import os
19 import sys
20
21
22 try:
23 from urlparse import parse_qsl
24 except:
25 from cgi import parse_qsl
26
27 import oauth2 as oauth
28
29 REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
30 ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
31 AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
32 SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
33
34 consumer_key = 'ozs9cPS2ci6eYQzzMSTb4g'
35 consumer_secret = '1kNEffHgGSXO2gMNTr8HRum5s2ofx3VQnJyfd0es'
36
37 if consumer_key is None or consumer_secret is None:
38 print 'You need to edit this script and provide values for the'
39 print 'consumer_key and also consumer_secret.'
40 print ''
41 print 'The values you need come from Twitter - you need to register'
42 print 'as a developer your "application". This is needed only until'
43 print 'Twitter finishes the idea they have of a way to allow open-source'
44 print 'based libraries to have a token that can be used to generate a'
45 print 'one-time use key that will allow the library to make the request'
46 print 'on your behalf.'
47 print ''
48 sys.exit(1)
49
50 signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
51 oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
52 oauth_client = oauth.Client(oauth_consumer)
53
54 print 'Requesting temp token from Twitter'
55
56 resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'GET')
57
58 if resp['status'] != '200':
59 print 'Invalid respond from Twitter requesting temp token: %s' % resp['status']
60 else:
61 request_token = dict(parse_qsl(content))
62
63 print ''
64 print 'Please visit this Twitter page and retrieve the pincode to be used'
65 print 'in the next step to obtaining an Authentication Token:'
66 print ''
67 print '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token'])
68 print ''
69
70 pincode = raw_input('Pincode? ')
71
72 token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
73 token.set_verifier(pincode)
74
75 print ''
76 print 'Generating and signing request for an access token'
77 print ''
78
79 oauth_client = oauth.Client(oauth_consumer, token)
80 resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % pincode)
81 access_token = dict(parse_qsl(content))
82
83 if resp['status'] != '200':
84 print 'The request for a Token did not succeed: %s' % resp['status']
85 print access_token
86 else:
87 print 'Your Twitter Access Token key: %s' % access_token['oauth_token']
88 print ' Access Token secret: %s' % access_token['oauth_token_secret']
89 print ''
90