它的危险创建一个令牌:无法连接'str'和'NoneType'对象

时间:2015-08-11 22:25:51

标签: python flask

我的Flask应用程序上有电子邮件确认功能。为此,我必须创建一个将在确认链接中的令牌。为了创建令牌,我正在使用它的危险:

from itsdangerous import URLSafeTimedSerializer
ts = URLSafeTimedSerializer(app.config["SECRET_KEY"])
token = ts.dumps(email, salt='email-confirm-key')
confirm = url_for('confirm', token=token, _external=True)

运行此操作后,我收到一条错误,指出cannot concatenate 'str' and 'NoneType' objects来自以下追溯:

Traceback (most recent call last):
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/myapp/views/confirmation.py", line 62, in resend
    activateEmail(email)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/myapp/views/functions.py", line 34, in activateEmail
    token = ts.dumps(email, salt='email-confirm-key')
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 566, in dumps
    rv = self.make_signer(salt).sign(payload)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 412, in sign
    return value + sep + self.get_signature(value)
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 347, in get_signature
    key = self.derive_key()
  File "/Users/pavsidhu/Documents/Web-Development/myapp/env/lib/python2.7/site-packages/itsdangerous.py", line 334, in derive_key
    self.secret_key).digest()
TypeError: cannot concatenate 'str' and 'NoneType' objects

我不确定问题是什么,因为email是一个字符串,盐也是一个。可能是什么问题呢?感谢。

1 个答案:

答案 0 :(得分:0)

问题是这一行:

ts = URLSafeTimedSerializer(app.config["SECRET_KEY"])

您的app.config["SECRET_KEY"]似乎未正确设置。如果用此

替换该行
ts = URLSafeTimedSerializer('test')

你会发现它有效。因此,您需要找出未正确设置app.config["SECRET_KEY"]的原因。