我知道 ascii/unicode 问题,并且看过这样的帖子。
然而,我在 GAE 上收到的错误消息并没有给我任何线索来解决这个问题。它似乎发生在flask_login 的cookie_digest 上?但为什么在那里?
有人可以指出我正确的方向吗?
Exception on /login [POST]
Traceback (most recent call last):
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1362, in full_dispatch_request
response = self.process_response(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask/app.py", line 1564, in process_response
response = handler(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 344, in _update_remember_cookie
self._set_cookie(response)
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 363, in _set_cookie
data = encode_cookie(str(session['user_id']))
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 444, in encode_cookie
return u'{0}|{1}'.format(payload, _cookie_digest(payload))
File "/base/data/home/apps/s~service/1-0-1-0.369408465659150157/lib/flask_login.py", line 688, in _cookie_digest
key = key.encode('utf-8') # ensure bytes
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 0: ordinal not in range(128)
编辑:
在挖掘 flask_login 之后,似乎它正在使用密钥发生。多奇怪!!!我已经对其进行了一些日志记录,以了解为什么只有我们的应用程序的一个用户会发生这种情况。而且它是可复制的,每次她尝试登录时,都会出现异常。但除了她,我无法重现。
def _cookie_digest(payload, key=None):
if key is None:
key = current_app.config['SECRET_KEY']
logging.info(u'key - cookie - {0}'.format(key))
if hasattr(key, 'encode'):
key = key.encode('utf-8') # ensure bytes
return hmac.new(key, payload.encode('utf-8'), sha1).hexdigest()