zai*_*hui 0 python flask itsdangerous
我正在遵循 \xe3\x80\x8eFlask Web Development\xe3\x80\x8f 的指南。
\n我想用itsdangerous生成token,但是出现了一些问题。这是我的代码:
def generate_confirmation_token(self, expiration=3600):\n s = Serializer(current_app.config[\'SECRET_KEY\'], expiration)\n return s.dumps({\'confirm\': self.id})\nRun Code Online (Sandbox Code Playgroud)\n\n是self.id一个 int 对象。
但不幸的是\xef\xbc\x8c“dumps”方法抛出一个TypeError:
\n\n File "/Users/zzx/projects/PycharmProjects/wurong/app/models.py", line 76, in generate_confirmation_token\n return s.dumps({\'confirm\': self.id})\n File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 566, in dumps\n rv = self.make_signer(salt).sign(payload)\n File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 353, in sign\n return value + want_bytes(self.sep) + self.get_signature(value)\n File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 347, in get_signature\n key = self.derive_key()\n File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 333, in derive_key\n return self.digest_method(salt + b\'signer\' +\nTypeError: unsupported operand type(s) for +: \'int\' and \'bytes\'\nRun Code Online (Sandbox Code Playgroud)\n\n我不知道为什么会出现这个问题,我只是按照书上的指导进行操作~
\n第二个参数Serializeris salt, not expiration。expiration根本不是一个论点Serializer,它是一个论点TimedSerializer.loadscalled max_age。
如果您想要一个过期的令牌,请在加载TimedSerializer令牌时使用并传递过期时间,而不是在创建令牌时。
def generate_confirmation(self):
s = TimedSerializer(current_app.secret_key, 'confirmation')
return s.dumps(self.id)
def check_confirmation(self, token, max_age=3600):
s = TimedSerializer(current_app.secret_key, 'confirmation')
return s.loads(token, max_age=max_age) == self.id
Run Code Online (Sandbox Code Playgroud)