Authentication credentials were not provided with djangorestframework-jwt
Asked Answered
I

1

5

I am trying to use django rest_framework_jwt. I can get it to generate a token but when i try and send it in the header to a protected view i get 'Authentication credentials were not provided.'

The format of the header I am sending is:

"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
Iulus answered 14/3, 2017 at 14:11 Comment(0)
W
9

From looking at the docs I would say you should remove the default TokenAuthentication from 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 protected api urls you must include the Authorization: JWT <your_token> header.

Waterline answered 14/3, 2017 at 14:22 Comment(1)
Got the two documentations mixed up. Correcting the header fixed the issue. ThanksIulus

© 2022 - 2024 — McMap. All rights reserved.