3

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 ?

4
  • Can you post you views code also? Commented Oct 31, 2019 at 18:48
  • Thanks for the advice, I have also used TokenAuthentication in the authentication_classes in every View Commented Nov 5, 2019 at 10:08
  • 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. Commented Feb 14, 2021 at 20:23
  • same problem here? did you find any solution? Commented Mar 15, 2021 at 16:50

2 Answers 2

6
from rest_framework import authentication
    
class TokenAuthentication(authentication.TokenAuthentication):
    authentication.TokenAuthentication.keyword = 'Bearer'
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.