Authentication credentials were not provided with djangorestframework-jwt

I am trying to use django rest_framework_jwt. I can get it to generate a token, but when I try to send it to the header in a secure view, I get "Authentication credentials were not provided."

The format of the header to send:

"Authorization": "Token SomeRandomToken"

settings.py

    INSTALLED_APPS = [
        ...
    rest_framework.authtoken
]

REST_FRAMEWORK = {
   'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework.authentication.TokenAuthentication',
       'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
   ),
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
   ),
}

views.py

class UserList(mixins.ListModelMixin,
               mixins.CreateModelMixin,
               generics.GenericAPIView):
    permission_classes = (permissions.IsAuthenticated,)
    authentication_classes = (JSONWebTokenAuthentication,)
    queryset = User.objects.all()
    serializer_class = UserSerializer
+4
source share
1 answer

From a look at docs, I would say that you should remove the default TokenAuthenticationfrom your AUTHENTICATION_CLASSES

   'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
   ),

Also, the header seems to have a different format:

Now, in order to access the protected api url, you must include the header Authorization: JWT <your_token>.

+5

Source: https://habr.com/ru/post/1672257/


All Articles