Facebook API – Part 1
Facebook is the social network. I know, there are many others out there, but Facebook is the market leader like google for search engines. If you use it or you want to ignore it, as a programmer, especially web programmer, you may have to know how the Facebook api works.
It’s hard to start
Facebook apps are really easy to program, if you know what you do. If you have no clue, it may be very hard. Before you can start, you have to understand how Facebook apps work. You have to know that there is a Facebook proxy.
Facebook Proxy
A Facebook app is displayed through a Facebook window. That means, that the HTML, JavaScript and CSS code is contained through an iFrame. This is done over the Facebook proxy. A request is done the following way:
- the user wants to display the app
- a request is sent to the Facebook proxy
- the proxy loads the requested app from a web server
- the HTML code of the loaded site is combined with the HTML code from Facebook
- the user gets the response
As you can see, you never really communicate with the actual site on the web server. You only talk to the proxy. For the user, this connection is invisible and for him it is not necessary. For a programmer, this may get very tricky. For the communication you have to remember to only communicate over the proxy and not directly. With this in mind, it is possible that not all GET and POST params get their way through the proxy! For a session remember: there are two different possible session, one for the communication with the proxy and one for the direct way. Store your session variables in the right session! You also have to know, that the proxy also sends his own params to your web server.
An example:
With a simple example I want to show you why the proxy may leads to problems for programmers. The sample app consists of to pages. Page 1 has a link to page 2. If you click that link, a GET param is set and sent to the page 2. Sounds easy doesn’t is?
Same example as Facebook app:
The users gets the first page with a link. He clicks that link and a request with a GET param is sent to the web server. Page number 2 is displayed. Where is the problem? The page is not displayed inside the Facebook frame. So for this you have to send the request not to the URL of your web app (www.myapp.com) instead you have to use the Facebook app URL (facebook.com/myapp/app_342956769). The problem is now, that it is not possible to send GET params over the proxy!
Documentation
If you want to use the Facebook api forget 90 percent of things that are found on the net. The main source for information is the documentation. The documentation has all information you need. I know, some parts are a bit difficult, but it is always current. There are many articles out which are already out of date and does not work with the current Facebook API. So spare time and look at the documentation first.
Basics
If you want to work with the Facebook API you have to use a SDK, which you can get from Facebook. It is recommended and it saves you much time. You can find SDKs for many different programming languages. You can use one of the following:
- JavaScript SDK
- PHP SDK
- iOS SDK
- Android SDK
- Third Party SDKs
like for example: .NET, Java, Python und Ruby SDKs
All these SDKs offer the same functionality. I have already worked with the PHP and JavaScript SDK and they work really good.