Il y avait deux raisons principales.
--Raison 1: n'est plus pris en charge. (2017 est la dernière version.)
Django-simplejwt peut obtenir access_token et refresh_token en même temps.
djangorestframework-jwt → djangorestframework-simplejwt
Tout d'abord, installez à partir de djangorestframework-simplejwt.
pip install djangorestframework-simplejwt
Structure de répertoire de django-rest-auth. Le fichier à corriger dans ce fichier est ʻutils.py`.
├── __init__.py
├── __init__.pyc
├── __pycache__
│   ├── __init__.cpython-38.pyc
│   ├── admin.cpython-38.pyc
│   ├── app_settings.cpython-38.pyc
│   ├── models.cpython-38.pyc
│   ├── serializers.cpython-38.pyc
│   ├── social_serializers.cpython-38.pyc
│   ├── urls.cpython-38.pyc
│   ├── utils.cpython-38.pyc
│   └── views.cpython-38.pyc
├── admin.py
├── admin.pyc
├── app_settings.py
├── app_settings.pyc
├── locale
│   ├── cs
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── de
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── es
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── ko
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── pl
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── ru
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   ├── tr
│   │   └── LC_MESSAGES
│   │       └── django.po
│   ├── zh_Hans
│   │   └── LC_MESSAGES
│   │       ├── django.mo
│   │       └── django.po
│   └── zh_Hant
│       └── LC_MESSAGES
│           ├── django.mo
│           └── django.po
├── models.py
├── models.pyc
├── registration
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── app_settings.cpython-38.pyc
│   │   ├── serializers.cpython-38.pyc
│   │   ├── urls.cpython-38.pyc
│   │   └── views.cpython-38.pyc
│   ├── app_settings.py
│   ├── app_settings.pyc
│   ├── serializers.py
│   ├── serializers.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── serializers.py
├── serializers.pyc
├── social_serializers.py
├── social_serializers.pyc
├── tests
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   ├── django_urls.cpython-38.pyc
│   │   ├── mixins.cpython-38.pyc
│   │   ├── settings.cpython-38.pyc
│   │   ├── test_api.cpython-38.pyc
│   │   ├── test_social.cpython-38.pyc
│   │   └── urls.cpython-38.pyc
│   ├── django_urls.py
│   ├── django_urls.pyc
│   ├── mixins.py
│   ├── mixins.pyc
│   ├── requirements.pip
│   ├── settings.py
│   ├── settings.pyc
│   ├── test_api.py
│   ├── test_api.pyc
│   ├── test_base.pyc
│   ├── test_social.py
│   ├── test_social.pyc
│   ├── urls.py
│   └── urls.pyc
├── urls.py
├── urls.pyc
├── utils.py # <-ceci
├── utils.pyc
├── views.py
└── views.pyc
En regardant à l'intérieur de ʻutils.py, il y a une fonction qui dit jwt_encode`, et cela ressemble à ceci, et changez ce code en code ci-dessous.
(https://django-rest-framework-simplejwt.readthedocs.io/en/latest/creating_tokens_manually.html)
def jwt_encode(user):
    try:
        from rest_framework_jwt.settings import api_settings 
    except ImportError:
        raise ImportError("rest_framework_simplejwt needs to be installed")
    jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
    jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
    payload = jwt_payload_handler(user)
    return jwt_encode_handler(payload)
↓
def jwt_encode(user):
    try:
        # from rest_framework_jwt.settings import api_settings <-- old version.
        from rest_framework_simplejwt.tokens import RefreshToken
    except ImportError:
        raise ImportError("rest_framework_simplejwt needs to be installed")
    # jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
    # jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
    # payload = jwt_payload_handler(user)
    refresh = RefreshToken.for_user(user)
    payload = {'refresh' : str(refresh), 'access': str(refresh.access_token)}
    return payload
Après cela, corrigez le code de serializers.py comme ci-dessous.
class JWTSerializer(serializers.Serializer):
    """
    Serializer for JWT authentication.
    """
    token = serializers.CharField()
    user = serializers.SerializerMethodField()
    def get_user(self, obj):
        """
        Required to allow using custom USER_DETAILS_SERIALIZER in
        JWTSerializer. Defining it here to avoid circular imports
        """
        rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
        JWTUserDetailsSerializer = import_callable(
            rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer)
        )
        user_data = JWTUserDetailsSerializer(obj['user'], context=self.context).data
        return user_data
↓
class JWTSerializer(serializers.Serializer):
    """
    Serializer for JWT authentication.
    """
    token = serializers.JSONField()
    user = serializers.SerializerMethodField()
    def get_user(self, obj):
        """
        Required to allow using custom USER_DETAILS_SERIALIZER in
        JWTSerializer. Defining it here to avoid circular imports
        """
        rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {})
        JWTUserDetailsSerializer = import_callable(
            rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer)
        )
        user_data = JWTUserDetailsSerializer(obj['user'], context=self.context).data
        return user_data
Before & After
Before
{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoyLCJ1c2VybmFtZSI6InVzZXIiLCJleHAiOjE1OTk5MzQ0NDYsImVtYWlsIjoiIn0.ddZRKpU76x66dkJVh0SEP3FZL1HHLv8XJqvaf8OEd5o",
  "user": {
    "pk": 2,
    "username": "user",
    "email": "",
    "first_name": "",
    "last_name": ""
  }
}
After
{
  "token": {
    "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYwMDAyMDYzMSwianRpIjoiNTEwNzhlZmU4MGY2NDNiYjkyYmUyMTlkMDc5OWM1ZTIiLCJ1c2VyX2lkIjoyfQ.eGzYcmAD5e7Oi_DnN9N0j0iMswiCnBc8Qwoeltnlhos",
    "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTk5OTM0NTMxLCJqdGkiOiI1ZTA1N2U5NThlM2I0NDgzODA4ZDkyNWFlODljMTMzYyIsInVzZXJfaWQiOjJ9.OFX7jYeco6uNK_WenOdO5jKJkN-RwMiwFZa1zhusoUA"
  },
  "user": {
    "pk": 2,
    "username": "user",
    "email": "",
    "first_name": "",
    "last_name": ""
  }
}
Suite à la vérification, il semble qu'il existe un nouveau package appelé dj-rest-auth, utilisez donc dj-rest-auth S'il vous plaît. Cela utilise simple-jwt.
Recommended Posts