无法使用REST Framework JWT进行身份验证

时间:2015-10-11 12:10:35

标签: python django django-rest-framework jwt

我可以成功请求令牌,并将其包含在新请求的标头中。但是,当我尝试访问受限制的视图时,我会继续获取 AttributeError: 'WSGIRequest' object has no attribute 'successful_authenticator'以及。{ AttributeError: 'module' object has no attribute 'InvalidTokenError'

这是我的虚拟限制视图:

class RestrictedView(APIView):
    permission_classes = (IsAuthenticated, )
    authentication_classes = (JSONWebTokenAuthentication, )

    def post(self, request):

        response_data = json.dumps({"authenticated": "mooh"})

        return HttpResponse(response_data, content_type='application/json')

post方法自然不会开始执行,因为身份验证存在问题。

以下是在Silk Profiler中检查后的传入请求的Authorization标头:

"AUTHORIZATION: JWT iOjE0NDQ1NjQ...."

我的settings.py包含以下内容:

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
    'FORM_METHOD_OVERRIDE': None,
    'FORM_CONTENT_OVERRIDE': None,
    'FORM_CONTENTTYPE_OVERRIDE': None
}

我很感激任何帮助,因为我一直坚持这个看似基本的问题。错误究竟意味着什么?

修改:原始问题已解决,如下所述。这是一个很愚蠢的错误。

1 个答案:

答案 0 :(得分:2)

事实证明,我只需将PyJWT安装更新到最新的1.4.0版本。