通过Facebook Connect FBML按钮登录后,是否可以通过PyFacebook检索和使用会话详细信息?我可以像通过Python触发登录一样使用auth.getSession()吗?

基本上,我正在尝试找出是否可以替换以下示例中的raw_input()之前的阶段:

import facebook

API_KEY     =   'xxx'
SECRET_KEY  =   'xxx'

facebook = facebook.Facebook(API_KEY, SECRET_KEY)

facebook.auth.createToken()

facebook.login()

# Login to the window, then press enter
print 'After logging in, press enter...'
raw_input()

# This is where FBML button should get me

facebook.auth.getSession()
print 'Session Key:   ', facebook.session_key
print 'Your UID:      ', facebook.uid

最佳答案

使用开发者Wiki上的Verifying the Facebook User页面,我跟踪了PyFacebook的getSession,validate_signature和getLoggedInUser方法,以及在Django的PyFacebook的example.py中找到的代码:

if 'session_key' in request.session and 'uid' in request.session:
    fb.session_key = request.session['session_key']
    fb.uid = request.session['uid']
else:

    try:
        fb.auth_token = request.GET['auth_token']
    except KeyError:
        # Send user to the Facebook to login
        return HttpResponseRedirect(fb.get_login_url())

    # getSession sets the session_key and uid
    # Store these in the cookie so we don't have to get them again
    fb.auth.getSession()
    request.session['session_key'] = fb.session_key
    request.session['uid'] = fb.uid


使用FBML按钮对Zope3进行了以下快速测试:

import facebook

API_KEY     =   'your_api_key'
SECRET_KEY  =   'your_secret_key'

fb = odb.facebook.Facebook(API_KEY, SECRET_KEY)

fb_uid = self.request[API_KEY+'_user']
fb_session_key = self.request[API_KEY+'_session_key']
fb_ss = self.request[API_KEY+'_ss']
fb_signature = self.request[API_KEY] #no suffix for signature

fb.session_key = fb_session_key
fb.uid = fb_uid

fb.friends.get() # Results in list of friends

10-07 22:00