本文主要介绍Python中,使用django jwt时报错:TypeError: decode() got an unexpected keyword argument 'verify'的解决。

报错信息:

    File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 497, in dispatch
self.initial(request, *args, **kwargs)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 414, in initial
self.perform_authentication(request)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/views.py", line 324, in perform_authentication
request.user
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/request.py", line 227, in user
self._authenticate()
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework/request.py", line 380, in _authenticate
user_auth_tuple = authenticator.authenticate(self)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework_simplejwt/authentication.py", line 40, in authenticate
validated_token = self.get_validated_token(raw_token)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework_simplejwt/authentication.py", line 94, in get_validated_token
return AuthToken(raw_token)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework_simplejwt/tokens.py", line 43, in __init__
self.payload = token_backend.decode(token, verify=verify)
File "/home/cjavapy/.local/share/virtualenvs/back-aSs_Rzmq/lib/python3.8/site-packages/rest_framework_simplejwt/backends.py", line 90, in decode
return jwt.decode(
TypeError: decode() got an unexpected keyword argument 'verify'

settings.py:

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication",
],
}

views.py:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from .models import User
from .serializers import UserSerializer, UserRegistrationSerializer
from django.http import Http404
from rest_framework import status
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist

class UserList(APIView):
permission_classes = [IsAuthenticated]
def get(self):
users = User.objects.all().exclude(is_staff=True).order_by("id")
serializer = UserSerializer(users, many=True)
return Response(serializer.data)
def post(self, request):
serializer = UserRegistrationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class UserDetail(APIView):
permission_classes = [IsAuthenticated]
@csrf_exempt
def get_object(self, pk):
try:
return User.objects.get(pk=pk)
except ObjectDoesNotExist:
raise Http404
@csrf_exempt
def get(self, request, pk):
user = self.get_object(pk=pk)
serializer = UserSerializer(user)
return Response(serializer.data)
@csrf_exempt
def put(self, request, pk):
user = self.get_object(pk)
serializer = UserSerializer(user, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@csrf_exempt
def patch(self, request, pk):
user = self.get_object(pk)
serializer = UserSerializer(user, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@csrf_exempt
def delete(self, request, pk):
user = self.get_object(pk)
user.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

解决方法:

PyJWT 2.2.0版本导致错误,需要隆级到PyJWT 2.2以下版本,例如:

PyJWT==2.1.0
djangorestframework-simplejwt==4.8.0

相关文档https://github.com/jpadilla/pyjwt/issues/698

推荐文档

相关文档

大家感兴趣的内容

随机列表