通过PyFacebook访问Facebook Connect FBML cookie?

时间:2009-10-23 14:17:09

标签: python facebook cookies

通过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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我不确定这是否能回答你的问题,而且,不可否认,我可能会引导你误入歧途,因为我是新手(在30分钟前使用pyfacebook推出了我自己的facebook auth后端):

首先,我安装了pyfacebook中间件(让我在任何视图中调用request.facebook):

'facebook.djangofb.FacebookMiddleware'

然后,我设置了一个带有facebook连接按钮的视图以及适当的设置,以便在授权完成时将其重定向到我需要的位置。

在我的facebook连接登录完成视图中,我通过将请求变量传递给request.facebook.check_session()来通过facebook连接cookie对facebook进行身份验证。如果它返回True,则该对象已准备好用于检索数据。

def myview(request):    
    fb = request.facebook    
    ready_to_rock = fb.check_session(request)
    if ready_to_rock:
        user_info = fb.users.getInfo([fb.uid],['first_name','last_name'])