用户未登录 - 会话cookie太大了?

时间:2017-12-17 13:02:17

标签: python google-app-engine flask

我在这里关注google app engine / python的身份验证教程:https://cloud.google.com/python/getting-started/authenticate-users

我确定我已经正确地遵循了所有内容,但是当我点击页面上的登录按钮时,系统会提示我使用google登录,但是当重定向回页面时,它会显示用户未登录。

我已经检查了本地服务器,它说:

UserWarning: The "session" cookie is too large: the value was 4755 bytes but the header required 26 extra bytes. The final size was 4781 bytes but the limit is 4093 bytes. Browsers may silently ignore cookies larger than this.

我不是百分百肯定这是我的问题,但这是唯一让我感到高兴的事情。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

是的,验证身份验证所需的所有数据都在cookie中,并且您在其中存储了太多信息。

您可以在_request_user_info() hook:

中减少为配置文件存储的内容
def _request_user_info(credentials):
    # ...
    resp, content = http.request(
        'https://www.googleapis.com/plus/v1/people/me')

    # ...
    session['profile'] = json.loads(content.decode('utf-8'))

不是存储整个字典,而是过滤json.loads()返回的字典,只保留应用程序真正需要的配置文件信息。那,或者将这些信息存储在其他地方,比如在memcached中(因此每次需要时都要检索它,并且它在memcached中仍然不可用)。

请参阅People resource documentation,了解session['profile']中存储的数据并选择您真正需要的数据。例如,教程只需要显示名称和图像URL:

profile = json.loads(content.decode('utf-8'))
session['profile'] = {'displayName': profile['displayName'], 'image': profile['image']}