Skip to content

Instantly share code, notes, and snippets.

@toffer
Created March 2, 2012 22:45
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 toffer/1962023 to your computer and use it in GitHub Desktop.
Save toffer/1962023 to your computer and use it in GitHub Desktop.
Use OAuth with slumber
#!/usr/bin/env python
# An example of modifying the session of a slumber.API instance,
# in order to use OAuth authentication and turn on verbose logging.
import requests
import slumber
import sys
# from https://github.com/maraujop/requests-oauth
from oauth_hook import OAuthHook
# These shouldn't be blank...
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_SECRET = ''
def twitter_test():
"""Access Twitter API with slumber."""
hook = OAuthHook(access_token=ACCESS_TOKEN,
access_token_secret=ACCESS_SECRET,
consumer_key=CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
header_auth=False)
sess = requests.session(hooks={'pre_request': hook}, config={'verbose': sys.stderr})
api = slumber.API('https://api.twitter.com/1/', append_slash=False)
api._store['session'] = sess
print api.statuses("home_timeline.json").get()
print api.statuses("home_timeline.json").get(count=1, include_entities='true')
def main():
twitter_test()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment