私はgoogle oauth2用の単純なフラスコアプリを作成しています。私はセッションが異なるユーザーのために既に登録されていることを示すときに問題に直面しています。それは完全にランダムです。時々ユーザーはoauthフローに入りますが、ほとんどの場合、資格情報を取得すると設定したテキストが返されます。 "Thank you for registering :)"
です。どこで私は間違っているのですか?Flaskセッションが更新されない
import flask
import logging
from oauth2client import client
from manage_auth import store_user_info, get_user_info
app = flask.Flask(__name__)
SCOPES = [
'email',
'profile',
'https://www.googleapis.com/auth/calendar',
# Add other requested scopes.
]
CLIENT_SECRET = 'client_secret_web.json'
APPLICATION_NAME = 'my_app_name'
@app.route('/')
def index():
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
return "Thank you for registering :)"
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets(
CLIENT_SECRET,
scope=' '.join(SCOPES),
redirect_uri=flask.url_for('oauth2callback', _external=True)
)
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
#get auth code
auth_code = flask.request.args.get('code')
#get credentials
credentials = flow.step2_exchange(auth_code)
flask.session['credentials'] = credentials.to_json()
#get info and store credentials
user_info = get_user_info(credentials)
email_address = user_info.get('email')
flask.session['user_id'] = email_address
if credentials.refresh_token is not None:
store_user_info(email_address, user_info, credentials)
return flask.redirect(flask.url_for('index'))
if __name__ == '__main__':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = True
app.run(host='0.0.0.0', port=8889)
Session.popが問題を解決しました。しかし、私は同時に3つのクライアントを開く場合、今、アプリがハングアップします。 – Rusty