In this article I will try to describe the problems that are not obvious to me and the questions that I encountered while trying to create the simplest application for Facebook.
So, the task was to write a web application for Facebook. To study the API and development principles, I decided to write a test application that will display a list of my friends and their statuses.
Since I was not actively using this social network, I had to start from the very beginning.
Study of the subject area
First of all, the section
“Get Started” on Facebook (hereinafter referred to as FB) was studied. In it, I found the answer to the question "how to write an application," but I did not quite understand what was needed and why. The next step was to study the
“Anatomy of an App” section with a description of the types of applications. And finally, I read the article
"How to create applications for Facebook" on Habré.
Everything I read happened in my head as the following picture:
- The web application in FB is on a specific page of the form apps.new.facebook.com/your_app_name (Canvas Page URL), on which the application is allocated most of the page.
- When requesting an application page, FB calls from the specified address (Callback URL) a script with certain parameters necessary for working with the API. The answer from your script FB displays on the page in the workspace.
- Your application can generate profile blocks for pasting into a user profile.
Choice of means of implementation
The
Facebook Developers Wiki has frameworks for a large number of languages. FB itself offers a library for PHP5.
Since the implementation language was not specified in the task, I chose Perl as the most studied and loved by me. In the future, I plan to “touch” Python and JS “just for fun”.
Implementation
To start development, we
install the WWW :: Facebook :: API module from
cpan.org on the
server .
')
The next step was an attempt to run an adapted test script from the example. This attempt failed because the FB did not send the session_key to my application, which is necessary for normal work with the FB API. After studying the examples in php, it was found that the required parameters are sent only if the user has allowed your application to access their data.
In the pearl barley module it implements the method
$fb->require_login($q);
to which the CGI object is passed. If there is no authorization, the method returns text to
FBML , which redirects the user in the right direction.
After checking for user authorization, we check the correctness of the transmitted data and set the session_key to communicate with the FB API.
my $ params = $ fb-> canvas-> validate_sig ($ q);
if ($ params -> {'user'})
{
$ fb-> session_key ($ params -> {'session_key'});
}
After these actions, the test script I successfully earned and with the use of FBML a list of my friends and their statuses appeared on the application page.
We continue the conversation
The next task is to place the profile box on the user profile page.
In the FB API, the
Profile.setFBML method is responsible for
this , to which the text must be passed in the profile parameter, which will be displayed in box on the Blocks tab, and in the profile_main parameter, the text that will be displayed in the block on the main page .
I faithfully passed these parameters:
$ fb-> profile-> set_fbml (
uid => $ params -> {'user'},
profile => 'blocks',
profile_main => 'main page'
);
After that, my torment began, which prompted me to write this article. The fact is that only the 'profile' parameter was set and the profile box appeared only on the “Blocks” tab, and the menu item that would allow placing the block on the main profile page was unavailable.
Studying the wiki and the FB application developer forum did nothing. Information on how to solve the problem was, but the advice was limited to the transfer of the required parameter “profile_main”, which I already had already passed on. Then there was a lot of Google and source codes).
The solution was found in the
bug to the module.
The fact is that FB changed its design in July 2008 and made a new REST server
api.new.facebook.com/restserver.php for the new interface, and the old address
api.facebook.com/restserver.php was specified in the module used
. .
After the line:
$fb->server_uri('http://api.new.facebook.com/restserver.php') ;
everything worked as it should.
Materials