程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairSerializer.get_token() 中没有属性 'decode'大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairserializer.get_token() 中没有属性 'decode'?

开发过程中遇到获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairserializer.get_token() 中没有属性 'decode'的问题如何解决?下面主要结合日常开发的经验,给出你关于获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairserializer.get_token() 中没有属性 'decode'的解决方法建议,希望对你解决获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairserializer.get_token() 中没有属性 'decode'有所启发或帮助;

我正在开发与选举相关的网络应用程序,并尝试使用 rest_framework_simplejwt.serializers 实现授权令牌概念,在运行此代码时,我遇到了与 str 类相关的错误,如下所示: AttributeError: 'str' object has no attribute 'decode' 从这部分代码: data['refresh'] = str(refresh)

class AuthTokenObtainserializer(TokenObtainPairserializer):
    """
    Seializer for th user authentication object.

    Returns
    -------
        Json: 'access' and 'token'
    """
    default_error_messages = {
        'no_active_account': _('No active account found with the given credentials')  # noqa: E501
    }

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fIElds['password'] = serializers.CharFIEld(
            style={'input_type': 'password'},trim_whitespace=false
        )
        self.fIElds['face_image'] = serializers.ImageFIEld()

    def valIDate(self,attrs):
        """
        ValIDates and authenticate the user.
        """
        citiZenship_number = attrs.get('citiZenship_number')
        password = attrs.get('password')
        face_image = attrs.get('face_image')

        face_ID = FacEIDAuthBACkend()
        user = face_ID.authenticate(
            citiZenship_number=citiZenship_number,password=password,face_ID=face_image
        )

        if user is None or not user.is_active:
            raise exceptions.AuthenticationFailed(
                self.error_messages['no_active_account'],'no_active_account',)

        update_last_login(None,user)

        data = {}
        refresh = self.get_token(user)

        data['refresh'] = str(refresh)

        data['access'] = str(refresh.access_token)

        return data

请帮我解决这个......

解决方法

当我在不同来源寻找解决方案时,我知道这个特定问题来自 PyJWT 包的最新版本,所以我必须将其降级到 v1.7.1,现在我不再遇到这个问题了。

来源:https://github.com/jazzband/django-rest-framework-simplejwt/issues/346

大佬总结

以上是大佬教程为你收集整理的获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairSerializer.get_token() 中没有属性 'decode'全部内容,希望文章能够帮你解决获取 AttributeError: 'str' 对象在 rest_framework_simplejwt.serializers.TokenObtainPairSerializer.get_token() 中没有属性 'decode'所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。