我正在遵循『Flask Web Development』的指南。
我想用 itsdangerous 生成一个令牌,但是出现了一些问题。这是我的代码:
def generate_confirmation_token(self, expiration=3600):
s = Serializer(current_app.config['SECRET_KEY'], expiration)
return s.dumps({'confirm': self.id})
是self.id
一个 int 对象。
但不幸的是,“dumps”方法抛出了一个 TypeError:
File "/Users/zzx/projects/PycharmProjects/wurong/app/models.py", line 76, in generate_confirmation_token
return s.dumps({'confirm': self.id})
File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 566, in dumps
rv = self.make_signer(salt).sign(payload)
File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 353, in sign
return value + want_bytes(self.sep) + self.get_signature(value)
File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 347, in get_signature
key = self.derive_key()
File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 333, in derive_key
return self.digest_method(salt + b'signer' +
TypeError: unsupported operand type(s) for +: 'int' and 'bytes'
我不知道为什么会出现这个问题,我只是按照我的书的指南进行的~