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

标签 django django-rest-framework django-rest-framework-jwt

我正在尝试使用 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/

相关文章:

python - django rest 框架查询集不排序

reactjs - 我需要在客户端检查 JWT 到期时间吗?

django-rest-framework - 我如何将简单 JWT 用于自定义用户模型

python - 警告 : cannot find svn location for distribute==0. 6.16dev-r0

python - NameError : name 'hasattr' is not defined - Python3. 6, Django1.11, Ubuntu16-17, Apache2.4, mod_wsgi

django-allauth:如何为我的域添加站点?

django - 在 django 中生成测试报告

python - 在整数字段的 django-rest-framework 验证器中强制整数类型

angularjs - 如何可靠地计算链接的点击次数

django - djoser 通过链接激活帐户