How do I use Facebook’s restserver.php?
Facebook’s API is crap. I am integrating Facebook Connect into our website and I have struggled to no end on working out how to do it! So as per usual as soon as I work something new out I post it here.
So, you want to use Facebook’s API restserver?
Nowhere in the docs do I see it clearly state how to make a GET request to restserver.php and actually have it work! So here goes!
Wait, first.. There is a Rails plugin that will help you out. I tried it and it conflicted with the current sites Javascript libraries and I found it difficult to use. I didn’t need or want half of what it gave me and I didn’t have full control over it in regard to my site and how it integrated in. I am of course talking about Facebooker. I think this is probably a growing problem with more and more sites being crafted out of 20 different rails plugins. The developers job is heading more to integration rather than creation. Not a criticism, just an observation.
See post Authenticate a Facebook Connect User for grabbing Facebook user id and authentication.
I created some methods to do the grunt work
add_facebook_defaults_and_method(method,params)
sorted_args = params.collect{|a|a[0].to_s+‘=’+a[1].to_s}.sort
sig = Digest::MD5.hexdigest(sorted_args.join+APP_SECRET)
sig_args = sorted_args.join(‘&’)+‘&sig=’+sig
res = Net::HTTP.get_response(URI.parse("http://api.new.facebook.com/restserver.php?"+sig_args))
puts res.body
JSON.parse(res.body)
end
def self.add_facebook_defaults_and_method(method,params)
params[:method] = method
params[:call_id] = Time.now.to_f.to_s
params[:api_key] = APP_ID
params[:v] = "1.0"
params[:format] = "json"
params
end
def self.get_names(uids, session_key)
response = Facebook.post("users.getinfo",{:fields=>"name",:uids=>uids.join(‘,’),:session_key=>session_key})
uids.each do |uid|
Rails.cache.write("facebook_user_name/#{uid}", response, :expires_in => 24.hours)
puts "Facebook #{uid} username cached"
end
response
end
The important bits are this.
Every GET request sent to http://api.new.facebook.com/restserver.php (changed from http://api.facebook.com/restserver.php) needs the following things
- method=<something.something> (method=User.getInfo)
- v=1.0
- api_key=<APP API_KEY> (api_key=3261d0c86fae5b363c2bfa872253887f)
- call_id=TimeStamp of NOW (call_id=1237760132) this is basically a number which always increments with each call, current time in milliseconds does fine.
- sig=MD5Hash (sig=8e719ba4d1506b2ac9551bfd2b0d42b3)
Which looks like the below
http://api.facebook.com/restserver.php?method=User.getInfo&api_key=3261d0c86fae5b363c2bfa872253887f&call_id=1237760132&fields=first_name&uids=705926010&v=1.0&sig=8e719ba4d1506b2ac9551bfd2b0d42b5
The sig needs to be computed in the same manner as in the Authentication.
- Put all params into an array, including the method=<whatever>
- Sort, join, add APP_SECRET to end, MD5Hash it
- Append that hash to end of params as sig=MD5Hash like below
http://api.facebook.com/restserver.php?method=User.getInfo&api_key=3261d0c86fae5b363c2bfa872253887f&call_id=1237760132&fields=first_name&uids=705926010&v=1.0&sig=8e719ba4d1506b2ac9551bfd2b0d42b5
The given example isn’t valid for the method it is calling, but it gives an idea. To make it valid it needs some more params, the uids and the session_key. When calling this method you should already have the session_key from the user via the cookies.
NOTE
Facebook Policy only allows you to keep user information for 24 hours.
To do this I am using memcached with a 24 hour timeout on it. It seems to be working fine and it doesn’t require the database to be hit at all.
Related Posts
In: Uncategorized · Tagged with: 24 hour cache, Facebook API, facebook connect, GET Request, RestServer
on January 29, 2010 at 5:46 am
Permalink
Dobrý den,
zajímalo by mě proč nefunguje Facebook,respektive funguje jen částečně.Když se na Facebook přihlásím zobrazí se mi hlavní menu a poté i profil,ale když si chci prohlédnout fotky nebo vstoupit a nějakou hru,zobrazí se prázdná stránka a to vždy.Trvá to už druhý den.Předem děkuji za odpověď.
David Benko,Czech Republic.
on February 7, 2010 at 5:42 am
Permalink
Facebook Is not working very well.We are all pissed.It’s not anyfun anymore.
on February 25, 2010 at 7:23 pm
Permalink
U really made my day.. Thank u very much..
on March 12, 2010 at 3:50 am
Permalink
Last 2 days all my FB email has been going into my (hotmail) junkmail, never had that before. I haven’t done or changed anything and waited to see if FB had a prob but so far appears e-1 else is ok. Last night I went into email/options/safe senders and checked. all I noticed was a pop link for FB so took one the junk mail address for FB and pasted it in Safe Senders and it replied it already has that! What do I or FB need to do to resolve this?
on March 12, 2010 at 1:14 pm
Permalink
Hey Tommy,
Sorry but I have no idea why that is happening or how to fix it. I find it odd asking here tho, good luck!