- Author:
- jezdez
- Posted:
- March 30, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
In case you ever use requests (or slumber) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you.
Use it like that (with requests):
auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
response = requests.get('http://api.foo.bar/v1/spam/', auth=auth)
or with slumber:
auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
api = slumber.API("http://api.foo.bar/v1/", auth=auth)
spam = api.spam().get()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from requests.auth import AuthBase
class TastypieApiKeyAuth(AuthBase):
"""
Sets the appropriate authentication headers
for the Tastypie API key authentication.
"""
def __init__(self, username, api_key):
self.username = username
self.api_key = api_key
def __call__(self, r):
r.headers['Authorization'] = 'ApiKey %s:%s' % (self.username, self.api_key)
return r
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Please login first before commenting.