Created
November 14, 2012 11:55
-
-
Save arslnb/4071722 to your computer and use it in GitHub Desktop.
Python Wrapper around Twitter API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
try: | |
from urlparse import parse_qsl | |
except: | |
from cgi import parse_qsl | |
import oauth2 as oauth | |
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token' | |
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token' | |
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize' | |
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate' | |
consumer_key = None | |
consumer_secret = None | |
if consumer_key is None or consumer_secret is None: | |
print 'You need to edit this script and provide values for the consumer key and secret. Register as a developer and register your "app" inorder to be allow this library to access and make requests off them. Lame, I know, but rules are rules.' | |
sys.exit(1) | |
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() | |
oauth_consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret) | |
oauth_client = oauth.Client(oauth_consumer) | |
print 'Requesting temp token from Twitter' | |
resp, content = oauth_client.request(REQUEST_TOKEN_URL, 'GET') | |
if resp['status'] != '200': | |
print 'Invalid response from Twitter requesting temp token: %s' % resp['status'] | |
else: | |
request_token = dict(parse_qsl(content)) | |
print '' | |
print 'Go here and get the pincode' | |
print '' | |
print '%s?oauth_token=%s' % (AUTHORIZATION_URL, request_token['oauth_token']) | |
print '' | |
pincode = raw_input('Pincode? ') | |
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) | |
token.set_verifier(pincode) | |
print '' | |
print 'Processing request for an access token' | |
print '' | |
oauth_client = oauth.Client(oauth_consumer, token) | |
resp, content = oauth_client.request(ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % pincode) | |
access_token = dict(parse_qsl(content)) | |
if resp['status'] != '200': | |
print 'The request for a Token failed: %s' % resp['status'] | |
print access_token | |
else: | |
print 'Your Twitter Access Token key: %s' % access_token['oauth_token'] | |
print ' Access Token secret: %s' % access_token['oauth_token_secret'] | |
print '' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This retrieves the access token and secret.