djangorestframework-jwt 未提供身份验证凭据

Authentication credentials were not provided with djangorestframework-jwt

我正在尝试使用 django rest_framework_jwt。我可以让它生成令牌,但是当我尝试将它发送到 header 到受保护的视图时,我得到 'Authentication credentials were not provided.'

我发送的header格式是:

"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

docs 来看,我会说你应该从你的 TokenAuthentication 中删除默认值 AUTHENTICATION_CLASSES

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

此外 header 似乎有不同的格式:

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