2
votes

I'm trying to rename the Rest_framework TokenAuthentication keyword from "Token" to "Bearer" as suggested in the Documentation I have subclassed the TokenAuthentication class like this:

in module: user/authentication.py

from rest_framework import authentication

class TokenAuthentication(authentication.TokenAuthentication):
    """
    Simple token based authentication.
    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:
    Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
    """

    keyword = 'Bearer'

in module app/settings.py

 REST_FRAMEWORK = {
     'DEFAULT_AUTHENTICATION_CLASSES': (
         'user.authentication.TokenAuthentication',
     ),
 }

It is still sending me a 401 Unauthorized when im using 'Authorization: Bearer ...token...' but not with 'Authorization: Token ...token...'

What am I doing wrong ?

2
Can you post you views code also? - Ayush Bansal
Thanks for the advice, I have also used TokenAuthentication in the authentication_classes in every View - Kape
Odd. This is all I needed to do to change the TokenAuthentication for my entire project. I did not need to add anything to the views. - Ryan H.
same problem here? did you find any solution? - Behnam Heydari

2 Answers

1
votes

You are missing one last step. Import your TokenAuthentication class that contains the new keyword in your View class instead of the default TokenAuthentication class.

4
votes

from rest_framework import authentication

class TokenAuthentication(authentication.TokenAuthentication):

authentication.TokenAuthentication.keyword = 'Bearer'