Created
August 16, 2012 13:13
-
-
Save swinton/3370018 to your computer and use it in GitHub Desktop.
Example of using grequests with oauth
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
#!/usr/bin/env python | |
""" | |
Example of using grequests with OAuth. | |
See: https://github.com/kennethreitz/grequests | |
""" | |
import json | |
import grequests, requests | |
from oauth_hook import OAuthHook | |
import settings | |
def grequests_oauth_client(access_token=None, access_token_secret=None, consumer_key=None, consumer_secret=None, header_auth=None): | |
oauth_hook = OAuthHook(access_token=access_token, access_token_secret=access_token_secret, | |
consumer_key=consumer_key, consumer_secret=consumer_secret, | |
header_auth=header_auth) | |
client = requests.session(hooks={'pre_request': oauth_hook}) | |
client.get = grequests.patched(client.get) | |
client.options = grequests.patched(client.options) | |
client.head = grequests.patched(client.head) | |
client.post = grequests.patched(client.post) | |
client.put = grequests.patched(client.put) | |
client.patch = grequests.patched(client.patch) | |
client.delete = grequests.patched(client.delete) | |
client.request = grequests.patched(client.request) | |
return client | |
def example(): | |
client = grequests_oauth_client(access_token=settings.OAUTH_TOKEN, access_token_secret=settings.OAUTH_SECRET, | |
consumer_key=settings.CONSUMER_KEY, consumer_secret=settings.CONSUMER_SECRET, | |
header_auth=False) | |
urls = 5 * ['http://api.twitter.com/1/account/rate_limit_status.json'] | |
rs = (client.get(u) for u in urls) | |
return [json.loads(response.content) for response in grequests.map(rs)] | |
if __name__ == "__main__": | |
print example() |
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
gevent==0.13.7 | |
greenlet==0.4.0 | |
grequests==0.1.0 | |
requests==0.13.6 | |
requests-oauth==0.4.1 |
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
OAUTH_TOKEN = "YOUR_OAUTH_TOKEN" | |
OAUTH_SECRET = "YOUR_OAUTH_SECRET" | |
CONSUMER_KEY = "YOUR_CONSUMER_KEY" | |
CONSUMER_SECRET = "YOUR_CONSUMER_SECRET" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment