Created
December 16, 2014 12:12
-
-
Save piotch/c79010e44c5e59a4f277 to your computer and use it in GitHub Desktop.
Attaches Django Rest Framework token auth header to the given Request object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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