简体   繁体   English

AttributeError: 'str' object 没有属性 'decode' python 错误

[英]AttributeError: 'str' object has no attribute 'decode' python error

this is my code这是我的代码

@app.route("/api/v1.0/login", methods=["GET"] )
def login():
    auth = request.authorization
    if auth:
        user = users.find_one( { "username" : auth.username } )
        if user is not None:
            if bcrypt.checkpw(bytes(auth.password, 'UTF-8'), user["password"]):
                token = jwt.encode({
                    'user' : auth.username,
                    'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])
                return make_response( jsonify({'token' : token.decode('UTF-8')}), 200)
            else: 
                return make_response(jsonify({"message" : "Bad password"} ) )
        else: 
            return make_response(jsonify({"message" : "Bad username" } ) )

    return make_response(jsonify({ "message" : "Authentication required"}))

and this is error这是错误

AttributeError: 'str' object has no attribute 'decode' AttributeError: 'str' object 没有属性 'decode'

If you are using PyJwt module, then there is no need to decode the token.如果您使用的是PyJwt模块,则无需解码令牌。 jwt.encode({some_dict}) returns the token you need. jwt.encode({some_dict})返回您需要的令牌。

this worked for me, i only returned the token (i initially had 'return token.decode('utf-8')' but later took the decode function off)这对我有用,我只返回了令牌(我最初有'return token.decode('utf-8')',但后来将解码 function 关闭)

   def _generate_jwt_token(self):
        dt = datetime.now() + timedelta(days=60)
        token = jwt.encode({
            'id': self.pk,
            'exp': dt.utcfromtimestamp(dt.timestamp())},
            settings.SECRET_KEY, algorithm='HS256')
        return token

In python 3, delete simply.decode('UTF-8')在 python 3 中,简单地删除.decode('UTF-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python AttributeError:“str”对象没有“decode”属性 - Python AttributeError: 'str' object has no attribute 'decode' python错误 - attributeError:'str'对象没有属性 - python error - attributeError: 'str' object has no attribute AttributeError:'str'对象没有属性'decode'Python3 - AttributeError: 'str' object has no attribute 'decode' Python3 Django 2 中的迁移错误; AttributeError: 'str' 对象没有属性 'decode' - Migrations error in django 2; AttributeError: 'str' object has no attribute 'decode' 获取“AttributeError:'str'对象没有属性'decode'” - Getting "AttributeError: 'str' object has no attribute 'decode'" AttributeError:“ str”对象没有属性“ decode” - AttributeError: 'str' object has no attribute 'decode' 有什么方法可以在python中解码str? AttributeError:“ str”对象没有属性“ decode” - are there any methods to decode str in python? AttributeError: 'str' object has no attribute 'decode' Python属性错误:AttributeError:'str'对象没有属性'to_csv' - Python attribute error: AttributeError: 'str' object has no attribute 'to_csv' AttributeError: 'str' 对象在 Python 中使用 kivy 没有属性 'fbind' 错误 - AttributeError: 'str' object has no attribute 'fbind' error using kivy in Python 如何修复“AttributeError: 'str' object has no attribute 'content'”python 错误 - how to fix for the "AttributeError: 'str' object has no attribute 'content' "python error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM