Login

Custom requests auth class for Tastypie API key authentication

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

  1. LazyPrimaryKeyRelatedField by LLyaudet 6 days, 4 hours ago
  2. CacheInDictManager by LLyaudet 6 days, 10 hours ago
  3. MYSQL Full Text Expression by Bidaya0 1 week ago
  4. Custom model manager chaining (Python 3 re-write) by Spotted1270 1 week, 6 days ago
  5. Django Standard API Response Middleware for DRF for modern frontend easy usage by Denactive 4 weeks, 1 day ago

Comments

Please login first before commenting.