CoreyMSchafer / code_snippets

MIT License
10.36k stars 17.55k forks source link

TypeError: unsupported operand type(s) for +: 'int' and 'bytes' #200

Open NightDev19 opened 1 year ago

NightDev19 commented 1 year ago

i think that return s.dumps({'user_id': self.id}).decode('utf-8') cant rea for newer version of python

SamBlackDev commented 1 year ago

I got it working by using the 'URLSafeTimedSerializer' module instead.

Steps to receive same results

  1. Remove 'expires_sec' arg from 'get_reset_token' function.
  2. remove the decode to utf-8 section.
  3. Add the 'max_age' arg to the 's.loads' command.

Before

from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
def get_reset_token(self, expires_sec=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')
@staticmethod
 def verify_reset_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

After (Updated)

from itsdangerous import URLSafeTimedSerializer as Serializer
def get_reset_token(self):
        s = Serializer(app.config['SECRET_KEY'])
        return s.dumps({'user_id': self.id})
@staticmethod
 def verify_reset_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token, max_age=1800)['user_id']
        except:
            return None
        return User.query.get(user_id)
Masayaelvin commented 1 year ago

go to you gmail account settings > security > 2 step verification>app >create new

you can name it whatever you want google will generate a password for your app you won't need to use your actual google password

copy the password and paste it in app[MAIL_PASS] and you'll be good to go. If your 2 step verification is off you'll have to turn it on hope this helps