Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Created September 25, 2010 14:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoiron/596865 to your computer and use it in GitHub Desktop.
Save jmoiron/596865 to your computer and use it in GitHub Desktop.
# twitter oauth example using urllib
#
# Many uses of the twitter api don't require authenticating as other users,
# but the documentation centers around it. In this example, we're using the
# twitter-provided access key & secret (keys['token']) rather than going
# through the handshake.
import json
import urllib2
# leah culver's oauth library
from oauth import oauth
# key,value tuples
keys = dict(
consumer = ('...', '...'),
token = ('...', '...'),
)
consumer = oauth.OAuthConsumer(*keys['consumer'])
access_token = oauth.OAuthToken(*keys['token'])
sig_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
def fetch(url, parameters=None):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
consumer, token=access_token, http_url=url, parameters=parameters
)
oauth_request.sign_request(sig_method, consumer, access_token)
headers = oauth_request.to_header()
headers['User-Agent'] = 'Example Twitter OAuth Agent'
request = urllib2.Request(url, headers=headers)
return json.loads(urllib2.urlopen(request).read())
if __name__ == '__main__':
from pprint import pprint
sample = 'http://api.twitter.com/1/account/rate_limit_status.json'
print "Getting %s" % sample
pprint(fetch(sample))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment