views:

905

answers:

2

Is it possible, having logged in via a Facebook Connect FBML button, to retrieve and use the session details via PyFacebook? Can i use auth.getSession() in the same way as if I'd fired the login via Python?

Basically I'm trying to work out if it's possible to replace the stages up to and including raw_input() in the example below:

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
+1  A: 

Using the Verifying the Facebook User page on the developer wiki I tracked down the PyFacebook getSession, *validate_signature* and getLoggedInUser methods and along with the code found in PyFacebook's 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

Got the following quick test for Zope3 working using the FBML button:

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
Jon Hadley
A: 

I'm not sure if this answers your question and, admittedly, I might be leading you astray because I'm new at this (just rolled my own facebook auth backend using pyfacebook about 30 minutes ago):

First, I installed the pyfacebook middleware (letting me call request.facebook in any view):

'facebook.djangofb.FacebookMiddleware'

Then, I setup a view with a facebook connect button and the appropriate settings for it to redirect where I need it to upon authorization completion.

In my facebook connect login done view, I authenticate to facebook with the facebook connect cookies by passing the request variable to request.facebook.check_session(). If it returns True, the object is ready to be used to retrieve data.

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'])
Thomas