我正在为google oauth2创建一个简单的烧瓶应用。我遇到问题,对于不同的用户,会话显示他们已经注册。这是完全随机的。有时用户进入oauth流程,但大多数时候它会返回我设置的文本,一旦我获得了凭据,
"Thank you for registering :)"
。我在哪里弄错了?
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)
答案 0 :(得分:0)
尝试在每次注销时弹出会话。希望这可能会有所帮助!