Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit a3f8ff2

Browse files
committed
Rewritten get_access_token.py to use requests-oauth
get_access_token.py now uses the requests oauth lib and no longer depends on oauth2. This closes #151
1 parent 0424031 commit a3f8ff2

File tree

1 file changed

+40
-48
lines changed

1 file changed

+40
-48
lines changed

get_access_token.py

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

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-
2317
import webbrowser
24-
import oauth2 as oauth
18+
from requests_oauthlib import OAuth1Session
2519

2620
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
2721
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
@@ -31,50 +25,48 @@
3125

3226
def get_access_token(consumer_key, consumer_secret):
3327

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)
3729

3830
print 'Requesting temp token from Twitter'
3931

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 ''
7870

7971

8072
def main():

0 commit comments

Comments
 (0)