Skip to content

Instantly share code, notes, and snippets.

@piotch
Created December 16, 2014 12:12
Show Gist options
  • Save piotch/c79010e44c5e59a4f277 to your computer and use it in GitHub Desktop.
Save piotch/c79010e44c5e59a4f277 to your computer and use it in GitHub Desktop.
Attaches Django Rest Framework token auth header to the given Request object.
from requests.auth import AuthBase
class DRFTokenAuth(AuthBase):
"""
Attaches Django Rest Framework token auth header to the given Request object.
http://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication
http://docs.python-requests.org/en/latest/user/authentication/#new-forms-of-authentication
"""
def __init__(self, api_key):
self.api_key = api_key
def __call__(self, r):
r.headers['Authorization'] = " Token %s" % self.api_key
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment