Login

Custom requests auth class for Tastypie API key authentication

Author:
jezdez
Posted:
March 30, 2012
Language:
Python
Version:
Not specified
Tags:
api auth tastypie
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. Tastypie MongoDB Resource by fatiherikli 2 years, 5 months ago
  2. Tastypie v0.9.11 LoginRequiredAuthorization by cotton 2 years, 6 months ago
  3. HTTP basic auth decorator by bthomas 6 years, 1 month ago
  4. logging tastypie middleware by lalutium 3 months, 4 weeks ago
  5. ad-hoc request authentication by mwicat 3 years, 10 months ago

Comments

Please login first before commenting.