Docsity

Programming in Facebook - Internet Interoperability - Lecture Slides, Slides of Information Security and Markup Languages

Partial preview of the text

Download Programming in Facebook - Internet Interoperability - Lecture Slides and more Slides Information Security and Markup Languages in PDF only on Docsity!

Programming in Facebook

What is Facebook?

Facebook Communication

user facebook.com application (UCT)

request for profile

profile

request for canvas

canvas

Facebook API use

update profile box, etc.

request for canvas content

canvas

What can the API do

  • Get a list of friend ids or friends who are running the same app
  • Get and set notifications of events
  • Set content of profile box
  • Get specific information on a user
  • Add to the Newsfeed for a user
  • etc.
See: http://developers.facebook.com/documentation.php?v=1.0&doc=

FQL

  • Facebook Query Language allows access to user database with language that is similar to SQL.
  • Example:
    • SELECT first_name FROM user WHERE uid=$params->{user}
    • To get the first name of a user based on the user- id

Creating an Application

  • What you need:
    • Web Server
    • API toolkit or roll-your-own
  • Add the Developer application to your profile.
  • Switch to the Developer application.
  • Apply for a key.

What does a typical application

do?

  • Check CGI parameters.
  • Authenticate using Facebook API.
  • Draw configuration Web page on canvas.
  • Allow user to update options โ€ฆ
  • Write FBML to profile box.
  • Save per-user state in database or in filesystem.

Example: Profile Box

The Code โ€“ index.pl

  • #!/usr/bin/perl
  • sample adapted from Facebook Perl API example

  • hussein suleman

  • 31 july 2007

  • use strict;
  • use CGI;
  • use WWW::Facebook::API;
  • use Data::Dumper;
  • use LWP::UserAgent;
  • use XML::DOM;
  • create FB object with keys included

  • my $facebook = WWW::Facebook::API->new(
  • api_key => "8a3b63fde6e900f853c51903e6689e5b",
  • secret => "f1818d69d7140fa0192b623b55e7da59",
  • app_path => "hussein_testb",
  • parse => 1,
  • );
  • remember to add proxy for UCT!

  • $facebook->ua->proxy ('http', 'http://cache.uct.ac.za:8080/');
  • sub main {
  • handler CGI parameters

  • my $q = new CGI;
  • print $q->header;
  • redirect("Must be called in facebook canvas")
  • unless $facebook->canvas->in_fb_canvas($q);
  • my $params = $facebook->canvas->validate_sig($q);
  • if ( $params->{user} ) {
  • Canvas takes care of setting up a session for us, no need to call the

  • auth methods.

  • $facebook->session_key( $params->{session_key} );
  • }
  • else {
  • User hasn't added app (could reject/display info/whatever)

  • (Or handle later when a user is needed).

  • }
  • my ( $action, $param ) = ( $q->path_info =~ m!^/(\w+)/?(.*)! );
  • trigger action depending on parameters passed to app

  • if ($action eq '')
  • { handle_index($param, $params); }
  • else
  • { print_error ("cannot understand request"); }
  • }
  • canvas drawing routine

  • sub handle_index {
  • my ( $param, $params ) = @_;
  • print "";
  • print "Watch My Students";
  • print "{user}"/> ";
  • list data from disk file with links to delete entries

  • print "{user}" useyou="false"/>'s Current Wat
  • ched Entries";
  • my $fbml = "Watch My Students

    Sneak peek at what the 1016 stude

  • nts are chatting about in the class forum ...

    ";
  • print "";
  • "'>Add this application.";
  • }
  • update profile box

  • $facebook->profile->set_fbml (markup => $fbml);
  • }
  • deal with users trying to use app without logging into FB

  • sub redirect {
  • div_error("Please go to facebook" );
  • exit;
  • }
  • general error handler

  • sub print_error {
  • print "", join( "", @_ ), "";
  • }
  • retrieve and parse an RSS feed

  • sub get_RSS
  • {
  • my $ua = new LWP::UserAgent();
  • $ua->timeout(10);
  • $ua->proxy ('http://cache.uct.ac.za:8080/');
  • my $response = $ua->get('http://banzai.cs.uct.ac.za/phpBB2-1016/rss.php');
  • my @results = ();
  • if ($response->is_success) {
  • my $parser = new XML::DOM::Parser;
  • my $doc = $parser->parse ($response->content);
  • foreach my $anitem ($doc->getElementsByTagName ('item')) Docsity.com
  • my $title = '';
  • foreach my $anode ($anitem->getElementsByTagName ('title'))
  • {
  • if ($anode->hasChildNodes == 1)
  • {
  • $title = $anode->getFirstChild->toString;
  • }
  • }
  • my $author = '';
  • foreach my $anode ($anitem->getElementsByTagName ('author'))
  • {
  • if ($anode->hasChildNodes == 1)
  • {
  • $author = $anode->getFirstChild->toString;
  • }
  • }
  • my $description = '';
  • foreach my $anode ($anitem->getElementsByTagName ('description'))
  • {
  • if ($anode->hasChildNodes == 1)
  • {
  • $description = $anode->getFirstChild->toString;
  • }
  • }
  • $description =~ s/<//go;
  • $description =~ s/[\n\r]//go;
  • push (@results, [$title, $author, $description]);
  • }
  • }
  • @results;
  • }
  • main();