What's new? | Help | Directory | Sign in
Google
minifb
Minimal Facebook API for Python
  
  
  
  
    
Search
for
Updated Jul 17, 2007 by shredwheat
Labels: Featured
ExampleCode  
Examples of use

The following code shows how to deal with most of type of transations with the facebook server.

import minifb
_FbApiKey = "xxxyyyzzz..."
_FbSecret = minifb.FacebookSecret("aaabbbccc...")


def ModPythonHandler(request):
    '''FBML Canvas page receives session info from POST'''
    arguments = minifb.validate(_FbSecret, request.read())
    if arguments["added"] != "1":
        return ServeIndexNonmember()
    else:
        session_key = arguments["session_key"]
        uid = arguments["user"]
        return ServeIndex(uid, session_key)


def UserAdded(request):
    '''Facebook callback when user has added application
        gets an auth_token through post that must be converted
        into a session_key. Then lookup and send stuff to Facebook'''
    # Parse and validate posted values
    arguments = minifb.validate(_FbSecret, request.read())
    auth_token = arguments["auth_token"]
    
    # Request session_key from auth_token
    result = minifb.call("facebook.auth.getSession",
                _FbApiKey, _FbSecret, auth_token=auth_token)
    uid = result["uid"]
    session_key = result["session_key"]

    # Lookup username and details
    usersInfo = minifb.call("facebook.users.getInfo",
                    _FbApiKey, _FbSecret, session_key=session_key,
                    call_id=True, fields="name,pic_square",
                    uids=uid) # uids can be comma separated list
    name = usersInfo[0]["name"]
    photo = usersInfo[0]["pic_square"]
    
    AddUserToDatabase(uid, name, photo)

    # Set the users profile FBML
    fbml = "<p>Welcome, new user, <b>%s</b></p>" % name
    minifb.call("facebook.profile.setFBML",
                _FbApiKey, _FbSecret, session_key=session_key,
                call_id=True, uid=uid, markup=fbml)

Comment by friendless.farrell, Oct 22, 2007

Oh thank you, I needed to know that.

Comment by friendless.farrell, Oct 24, 2007

But... I don't have an auth_token in my request... where do I get it from? The scenario is that I'm writing a game. The user has their turn and I want to notify the opponent that it's their turn. This is what I get in the request:

{'fb_sig_time': '1193220206.4659', 'fb_sig_added': '1', 'fb_sig_session_key': '2a8280f22e96941a96a47467-607767124', 'fb_sig_profile_update_time': '1193010529', 'fb_sig_user': '607767124', 'fb_sig_expires': '0', 'fb_sig': '...stuff...', 'fb_sig_api_key': '...stuff...', 'id': '3', 'fb_sig_friends': '902555369,etc', 'fb_sig_in_canvas': '1'}

Can I use that session_key? Where did it come from? I'm not getting this at all :-(. Thanks,

John


Sign in to add a comment