在Django Rest Framework中解码签名时出错

时间:2016-10-03 07:42:21

标签: python json django django-rest-framework jwt

我在我的API中使用JWT。但我无法通过JWT进行身份验证。我无法找到的问题是什么。谢谢你的帮助。

@api_view(['POST'])
@permission_classes((AllowAny,))
def UserLogin(request):
    try:
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        account = authenticate(username=username, password=password)

    except (User.DoesNotExist, User.PasswordDoesNotMatch):
            return Response({'message': 'Wrong credentials'}, status=400)
    if account is not None:
        if account.is_active:
            login(request, account)
            if request.user.is_superuser:
                    user_type = '0'
            elif request.user.is_instructor():
                    user_type = '1'
            elif request.user.is_student():
                    user_type = '2'
            else:
                    user_type = '3'

            payload = {
                    'user_type': user_type,
                    'username': username,
                    'exp': datetime.utcnow() + timedelta(seconds=JWT_EXP_DELTA_SECONDS)
                }
            jwt_token = jwt.encode(payload, JWT_SECRET, JWT_ALGORITHM)
            return Response({'token': jwt_token.decode('utf-8')})

有效载荷数据返回

{
  "user_type": "0",
  "username": "bus",
  "exp": 1475480008
}

这对我来说已经足够了。但是当我请求其他API网址时,它会返回

{
  "detail": "Error decoding signature."
}

0 个答案:

没有答案