Google App Engine-Python-Flask-oauth2client-“无效的请求状态”错误

时间:2018-07-14 18:21:26

标签: python google-app-engine google-oauth2

我正在尝试编写一个简单的Google App Engine应用程序,以提取我的Google日历事件。这是我的代码:

import os

from apiclient.discovery import build
from oauth2client.contrib.flask_util import UserOAuth2
from flask import Flask

app = Flask(__name__)
app.secret_key = 'secret'

app.config['GOOGLE_OAUTH2_CLIENT_ID'] = 'xxxxx.apps.googleusercontent.com'
app.config['GOOGLE_OAUTH2_CLIENT_SECRET'] = 'secret'

oauth2 = UserOAuth2(app)

@app.route('/')
@oauth2.required(scopes=['https://www.googleapis.com/auth/calendar.readonly'])
def home():
    http = oauth2.http()
    service = build('calendar', 'v3', http=http)
    result = service.events().list(calendarId='primary').execute()
    return "Hello, {} ({})".format(oauth2.email, oauth2.user_id)

验证之后,我在浏览器中看到"Invalid request state"。网址是:

http://localhost:8080/oauth2callback?state=%7B%22csrf_token%22:+%22863627a865be2113450700ee52763f37522e62b5c97e942375ec5377ab0dc390%22,+%22return_url%22:+%22http://localhost:8080/%22%7D&code=4/AADbqpPPhHQlv_lYPOCe9Q8DsO6XHzD-uJXKQfd9yYO7dMSQAj7rcdLdBED2LqQsJ7x0eBZTgPLFPtLpPC5o7lw#

日志:

2604:2000:8147:1b00:7d6f:b191:cd7a:321a - - [17/Jul/2018:16:41:03 -0400] "GET / HTTP/1.1" 302 540 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" "xxx.appspot.com" ms=NaN cpu_ms=3121 cpm_usd=6.034899999999999e-8 loading_request=1 instance=00c61b117cdb6fb6db16f355621aff162c3bd9b98966a1d2514536dd6fba1692ec1430e596 app_engine_release=1.9.54 trace_id=f6f416d28263cd8f184f8f17707dbce8
2604:2000:8147:1b00:7d6f:b191:cd7a:321a - - [17/Jul/2018:16:41:05 -0400] "GET /oauth2authorize?scopes=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&scopes=email&return_url=https%3A%2F%2Fxxx.appspot.com%2F HTTP/1.1" 302 5280 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" "xxx.appspot.com" ms=NaN cpu_ms=28 cpm_usd=5.90085e-7 loading_request=0 instance=00c61b117cdb6fb6db16f355621aff162c3bd9b98966a1d2514536dd6fba1692ec1430e596 app_engine_release=1.9.54 trace_id=2ecc3838c2381b5ca92d3301eea94675
2604:2000:8147:1b00:7d6f:b191:cd7a:321a - - [17/Jul/2018:16:41:09 -0400] "GET /oauth2callback?state=%7B%22csrf_token%22:+%22a827600a31bdb8e006d7bc2eab173ef2f7956e4828ba36ff5c06c298e73ff89b%22,+%22return_url%22:+%22https://xxx.appspot.com/%22%7D&code=4/AACb5iinwvIm2Suc5gYw-LHAqD3yYHS9TUfhubnEFJ70tRPSu24eWWepc2eetThc0g2_S1H7OvMVNVLr_zHoRPE HTTP/1.1" 400 101 - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" "xxx.appspot.com" ms=NaN cpu_ms=7 cpm_usd=1.1287e-8 loading_request=0 instance=00c61b117cdb6fb6db16f355621aff162c3bd9b98966a1d2514536dd6fba1692ec1430e596 app_engine_release=1.9.54 trace_id=6a1e42f8ea14328e8a575eb109021550

一些研究(Invalid request state: oauth2 flask)表明cookie大小可能是问题所在?

Cookie size

如何以其他方式存储会话信息?谢谢!

0 个答案:

没有答案