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