存储facebook登录会话

时间:2016-10-12 18:48:29

标签: python flask facebook-login

我通过facebook成功验证了用户并在def fbconnect()

中获取了他们的名字
print "username" ,login_session['username']
session.name=login_session['username']
print "session.name",session.name

但是在我的html中,当我尝试检查用户是否登录时

{% if session.name != '' %}
<a href="{{url_for('showLogin')}}">Click Here to Login</a>
{% else %}
<a href="{{url_for('fbdisconnect')}}">>Welcome {{session.name}}, Logout</a>
{% endif %}

它没有转到欢迎名称部分,因为session.name是&#39;&#39;我在这里错过了什么?感谢。

这是我的python东西

  @app.route('/fbconnect', methods=['POST'])
def fbconnect():
    if request.args.get('state') != login_session['state']:
        print "state not equal to login_session"
        response = make_response(json.dumps('Invalid state parameter.'), 401)
        response.headers['Content-Type'] = 'application/json'
        return response
    access_token = request.data
    print "access token received %s " % access_token

    app_id = json.loads(open('fb_client_secrets.json', 'r').read())[
        'web']['app_id']
    app_secret = json.loads(
        open('fb_client_secrets.json', 'r').read())['web']['app_secret']
    url = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s' % (
        app_id, app_secret, access_token)
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]

    # Use token to get user info from API
    userinfo_url = "https://graph.facebook.com/v2.4/me"
    print "userinfo_url", userinfo_url
    # strip expire tag from access token
    token = result.split("&")[0]
    print "token", token


    url = 'https://graph.facebook.com/v2.4/me?%s&fields=name,id,email' % token
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    # print "url sent for API access:%s"% url
    # print "API JSON result: %s" % result
    data = json.loads(result)
    login_session['provider'] = 'facebook'
    login_session['username'] = data["name"]
    login_session['email'] = data["email"]
    login_session['facebook_id'] = data["id"]

    # The token must be stored in the login_session in order to properly logout, let's strip out the information before the equals sign in our token
    stored_token = token.split("=")[1]
    login_session['access_token'] = stored_token

    # Get user picture
    url = 'https://graph.facebook.com/v2.4/me/picture?%s&redirect=0&height=200&width=200' % token
    h = httplib2.Http()
    result = h.request(url, 'GET')[1]
    data = json.loads(result)

    login_session['picture'] = data["data"]["url"]

    # see if user exists
    user_id = getUserID(login_session['email'])
    if not user_id:
        user_id = createUser(login_session)
    login_session['user_id'] = user_id

    output = ''
    output += '<h1>Welcome, '
    output += login_session['username']

    output += '!</h1>'
    output += '<img src="'
    output += login_session['picture']
    output += ' " style = "width: 300px; height: 300px;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;"> '

    flash("Now logged in as %s" % login_session['username'])

    print "username" ,login_session['username']
    session['name']=login_session['username']
    print "session['name']",session['name']

    return output

如果我将html更改为

{% if login_session['username'] != '' %}

然后它说UndefinedError:&#39; login_session&#39;未定义

1 个答案:

答案 0 :(得分:0)

现在正在使用session['username']

我不得不将一些代码从一个页面移动到另一个页面以使其工作。

感谢您的帮助。