Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created December 13, 2012 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aparrish/4277791 to your computer and use it in GitHub Desktop.
Save aparrish/4277791 to your computer and use it in GitHub Desktop.
Here's how to make an OAuth1-signed request to the Twitter API with the requests library.
# Run on the command line like so:
# $ python your_consumer_key your_consumer_secret your_access_token your_token_secret
import requests
from requests.auth import OAuth1
import sys
consumer_key, consumer_secret, access_token, token_secret = \
[unicode(x) for x in sys.argv[1:]]
# signature type "query" for GET requests
oauth = OAuth1(consumer_key, consumer_secret, access_token, token_secret,
signature_type="query")
resp = requests.get('https://api.twitter.com/1.1/statuses/home_timeline.json',
auth=oauth)
print resp.status_code
print resp.headers
print resp.json
# signature type "body" for POST requests
oauth = OAuth1(consumer_key, consumer_secret, access_token, token_secret,
signature_type="body")
resp = requests.post('https://api.twitter.com/1.1/favorites/create.json',
auth=oauth, data={'id': '268913321816780801'})
print resp.status_code
print resp.headers
print resp.json
@aparrish
Copy link
Author

aparrish commented Jan 6, 2013

n.b. this is out of date, now that the OAuth1 thingy isn't included with requests any more. should be able to adapt this to use https://github.com/requests/requests-oauthlib without many changes, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment