How to resume flask.session object when calling oath2

I am writing a Sina Weibo client through oauth2 to add authentication to the current user who has already connected to my own site account, since OAuth2 uses the redirect-callback mechanism, it seems that after this procedure and in the callback view handler, flask.session - This is a completely new facility. Thus, I lost the current user login status.

but in the same browser (e.g. Firefox) to add a new tab and visit the homepage of my site (www.funfunsay.com), the session object still exists!

So, in one instance of the browser there are two flash drives.

I wrote a very simple module, it goes well with Sina weibo, except that I lost the old session.

# -*- coding: utf-8 -*-

from flask import (Flask, render_template, current_app, request,
                   flash, url_for, redirect, session, g, abort)


# For import *
__all__ = ['create_app']

app = Flask(__name__)
app.config['DEBUG'] = True
app.secret_key = 'secret key'

@app.before_request
def before_request():
    print "before request:", session
    #<1st output>for first visit www.funfunsay.com, the output is: before request: <SecureCookieSession {}>
    #<3rd output>for callback from sina, the output is the same

@app.after_request
def after(response):
    print "after request:", session
    #<2nd output>for first visit www.funfunsay.com, the output is: after request: <SecureCookieSession {'testinfo': 'abc'}*>
    return response


@app.route('/')
def hello_world():
    print "request:", request
    login_uri = 'https://api.weibo.com/oauth2/authorize?redirect_uri=http%3A//funfunsay.com/connect/sina/callback&response_type=code&client_id=3921006605&display=default'

    session['testinfo'] = 'abc' #a key-value for test

    return redirect(login_uri)

@app.route("/connect/sina/callback")
def connect_sina_callback():
    print "connect_sina_callback: ", session
    #<4th output>will output: connect_sina_callback:  <SecureCookieSession {}>
    return 'Callback success!'

if __name__ == '__main__':
    app.run('0.0.0.0', 80)

PS: "127.0.0.1 www.funfunsay.com" .

+3
2

. "www.my-web-site.com", "my-web-site.com", "www.my-web-site.com". !

+2

, , , IP-, . . , http://localhost:5000, , http://127.0.0.1:5000... .

, , ... , session.

OAuth, , URL- ( , , , OAuth ) , . , OAuth Github API URL- http://localhost:5000/callback, http://127.0.0.1:5000, .

, ( , localhost): , , URL- OAuth ( Facebook, Twitter Github OAuth API).

0

All Articles