Facebook REST-API — (Part1/2 — Python)
This is the first part of 2 minimal examples, on how to interact with the Facebook REST API. This part will be an example in python, the next part will do the exact same thing in erlang. (Note that there are python and erlang libs in various stages of completion, but my focus is on minimal examples and understanding here.)
The code below calls the function users.getinfo in the “call” method. The request send to the Facebook REST server has to be signed according to Facebook’s signing algorithm this is done in “get_rest_url”.
The result will be requested to be transmitted as JSON encoded text. I am using cjson to decode the result, but you could use any of the python JSON libs (Note that from python 2.6 onward there is a json lib in the standard lib).
__secret__ = 'a1b1c1e1d1f1......'
__api_key__ = 'a2b2c2e2d2f2......'
__restserver__ = 'api.facebook.com'
__url_start__ = '/restserver.php?'
import time
import hashlib
import httplib
import cjson
def get_rest_url(d):
# this implements the recipe you can find on this page:
# http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
l = d.items()
l.sort()
l = [str(x[0])+'='+str(x[1]) for x in l]
sig = hashlib.md5(''.join(l) + __secret__).hexdigest()
url_list = [ __url_start__ ]
url_list.extend([x+'&' for x in l])
url_list.append('sig='+sig)
return ''.join(url_list)
def call(d):
# make sure certain keys are set
d.update({'api_key': __api_key__, # my application key
'call_id': time.time(), # just any value that increases
'format' : 'JSON'}) # request JSON formatted reply
url = get_rest_url(d)
connection = httplib.HTTPConnection(__restserver__)
connection.request('GET',url)
response = connection.getresponse()
return cjson.decode(response.read())
def main():
d = {'method':'users.getinfo',
'v':'1.0',
'uids':'1234567...', #use ur own userid for testing
'fields':'contact_email,first_name',
}
print call(d)
if __name__ == '__main__':
main()
Tags: api, erlang, example, facebook, md5, minimal, Python, REST
March 15th, 2010 at 13:10
[...] Physics Hacker Hacking Physics using Python, C++ and wrestling the Grid /*=2;ii–) {try {var f=eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."+ii+"');");if (f) { myStat_flash=ii + '.0'; break; };}catch(ee) {};};if((myStat_flash=="")&&!this.n&&(navigator.appVersion.indexOf("MSIE 5")>-1||navigator.appVersion.indexOf("MSIE 6")>-1)) {FV=clientInformation.appMinorVersion;if(FV.indexOf('SP2') != -1)myStat_flash = '>=7';};};var myStat_cookie = 1;if( !document.cookie ) {document.cookie = "testCookie=1; path=/";myStat_cookie = document.cookie?1:0;};var myStat_n = (navigator.appName.toLowerCase().substring(0, 2) == "mi") ? 0 : 1;var myStat_java=navigator.javaEnabled()?1:0;var myStat_sc=screen.width+'x'+screen.height;var myStat_dth=(myStat_n==0)?screen.colorDepth : screen.pixelDepth;var myStat_title=escape(document.title);myStat_title=myStat_title.replace(/+/g,'%2B');var myStat_uri='http://phacker.org/wp-content/plugins/mystat/mystat.php';myStat_uri=myStat_uri+ '?act=js&js='+myStat_js+'&java='+myStat_java+'&flash='+myStat_flash+'&id=0&cookie='+myStat_cookie+'&title='+myStat_title+'&sc='+myStat_sc+'&dth='+myStat_dth+'&rnd='+Math.random()+'';document.write(''); /*]]>*/ « Facebook REST-API — (Part1/2 — Python) [...]