我正在尝试使用 django rest_framework_jwt。我可以让它生成一个 token ,但是当我尝试将其在 header 中发送到 protected View 时,我得到“未提供身份验证凭据”。
我发送的 header 格式是:
"Authorization": "Token SomeRandomToken"
设置.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',
),
}
View .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',
),
此外,标题似乎有不同的格式:
Now in order to access protected api urls you must include the
Authorization: JWT <your_token>
header.
关于django - djangorestframework-jwt 未提供身份验证凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788301/