Package deefuzzer :: Package deefuzzer :: Package tools :: Module get_access_token
[hide private]
[frames] | no frames]

Source Code for Module deefuzzer.deefuzzer.tools.get_access_token

 1  #!/usr/bin/python2.4 
 2  # 
 3  # Copyright 2007 The Python-Twitter Developers 
 4  # 
 5  # Licensed under the Apache License, Version 2.0 (the "License"); 
 6  # you may not use this file except in compliance with the License. 
 7  # You may obtain a copy of the License at 
 8  # 
 9  #     http://www.apache.org/licenses/LICENSE-2.0 
10  # 
11  # Unless required by applicable law or agreed to in writing, software 
12  # distributed under the License is distributed on an "AS IS" BASIS, 
13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14  # See the License for the specific language governing permissions and 
15  # limitations under the License. 
16   
17   
18  import os 
19  import sys 
20   
21  # parse_qsl moved to urlparse module in v2.6 
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