Created
December 13, 2012 16:42
-
-
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.
This file contains 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
# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.