2009-05-08

Facebook python authentication gateway

Edit: it occurred to me what I have below is the basics of a thin facebook api wrapper. I might make it into one at some point in the future.



If you don't know what this does, you don't need it. Hope this helps some one. Written because pyfacebook is broken, always returns error 100.




FB_API_HOST="api.facebook.com"
FB_API_PATH="/restserver.php"

def get_session(auth_token):
params={
"api_key":FB_API_KEY,
"v":"1.0",
"auth_token":auth_token,
"generate_session_secret":1,
"method":"auth.getSession",
}

sorted = params.items()
sorted.sort(key=lambda x:x[0])

str_to_hash = ''.join(["%s=%s"%(x[0], x[1]) for x in sorted])
str_to_hash += FB_API_SECRET

md5 = hashlib.md5()
md5.update(str_to_hash)

sig = md5.hexdigest()

params["sig"] = sig

encoded_params = urllib.urlencode(params)
headers = {
"Content-type":"application/x-www-form-urlencoded",
}

conn = httplib.HTTPConnection(FB_API_HOST)
conn.request("POST", FB_API_PATH, encoded_params, headers)

response = conn.getresponse()

print response.status, response.reason
return response.read()


This Works For Me when I use it with iphone facebook-connect client:



[FBSession sessionForApplication:myApiKey getSessionProxy:myURL delegate:self];


Cheers,

Steve