NAME
WWW::Facebook::API - Facebook API implementation
VERSION
This document describes WWW::Facebook::API version 0.4.6
SYNOPSIS
use WWW::Facebook::API;
# @ENV{qw/WFA_API_KEY WFA_SECRET WFA_DESKTOP/} are the initial values,
# so use those if you only have one app and don't want to pass in values
# to constructor
my $client = WWW::Facebook::API->new(
desktop => 0,
api_key => 'your api key',
secret => 'your secret key',
);
# Change API key and secret
print "Enter your public API key: ";
chomp( my $val = <STDIN> );
$client->api_key($val);
print "Enter your API secret: ";
chomp($val = <STDIN> );
$client->secret($val);
# not needed if web app (see $client->canvas->get_fb_params)
$client->auth->get_session( $token );
use Data::Dumper;
my $friends_perl = $client->friends->get;
print Dumper $friends_perl;
my $notifications_perl = $client->notifications->get;
print Dumper $notifications_perl;
# Current user's quotes
my $quotes_perl = $client->users->get_info(
uids => $friends_perl,
fields => ['quotes']
);
print Dumper $quotes_perl;
$client->auth->logout;
DESCRIPTION
A Perl implementation of the Facebook API, working off of the canonical Java and PHP implementations. By default it uses JSON::Any to parse the response returned by Facebook's server. There is an option to return the raw response in either XML or JSON (See the parse
method below). As the synopsis states, the following environment variables are used to set the defaults for new instances:
WFA_API_KEY
WFA_SECRET
WFA_SESSION_KEY
WFA_DESKTOP
Additionally, for each instance that is created, the following environment variables are used if no values are set:
WFA_API_KEY_APP_PATH
WFA_SECRET_APP_PATH
WFA_SESSION_KEY_APP_PATH
WFA_DESKTOP_APP_PATH
Where APP_PATH
is replaced by whatever $client->app_path returns, with all non-alphanumeric characters replaced with an underscore and all characters upcased (e.g., foo-bar-baz becomes FOO_BAR_BAZ).
SUBROUTINES/METHODS
- new( %params )
-
Returns a new instance of this class. You are able to pass in any of the attribute method names in WWW::Facebook::API to set its value:
my $client = WWW::Facebook::API->new( parse => 1, format => 'JSON', secret => 'application_secret_key', api_key => 'application_key', session_key => 'session_key', session_expires => 'session_expires', session_uid => 'session_uid', desktop => 1, api_version => '1.0', callback => 'callback_url', next => 'next', popup => 'popup', skipcookie => 'skip_cookie', ); $copy = $client->new;
NAMESPACE METHODS
All method names from the Facebook API are lower_cased instead of CamelCase.
- auth
-
For desktop apps, these are synonymous:
$client->auth->get_session( $client->auth->create_token ); $client->auth->get_session;
And that's all you really have to do (but see WWW::Facebook::API::Auth for details about opening a browser on *nix for Desktop apps).
get_session
automatically setssession_uid
,session_key
, andsession_expires
for$client
. It returns nothing.If the desktop attribute is set to false the
$token
must be the auth_token returned from Facebook to your web app for that user:if ( $q->param('auth_token') ) { $client->auth->get_session( $q->param('auth_token') ); }
get_session
automatically setssession_uid
,session_key
, andsession_expires
for$client
. It returns nothing.See WWW::Facebook::API::Auth for details.
- canvas
-
Work with the canvas. See WWW::Facebook::API::Canvas.
$response = $client->canvas->get_user( $q ) $response = $client->canvas->get_fb_params( $q ) $response = $client->canvas->get_non_fb_params( $q ) $response = $client->canvas->validate_sig( $q ) $response = $client->canvas->in_fb_canvas( $q ) $response = $client->canvas->in_frame( $q )
- events
-
events namespace of the API (See WWW::Facebook::API::Events). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->events->get( uid => 234233, eids => [23,2343,54545] ); $response = $client->events->get_members( eid => 233 );
- fbml
-
fbml namespace of the API (See WWW::Facebook::API::FBML): All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->fbml->set_ref_handle( handle => '', fbml => ''); $response = $client->fbml->refresh_img_src( url => ''); $response = $client->fbml->refresh_ref_url( url => '');
- fql
-
fql namespace of the API (See WWW::Facebook::API::FQL):
$response = $client->fql->query( query => 'FQL query' );
- feed
-
feed namespace of the API (See WWW::Facebook::API::Feed). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->feed->publish_story_to_user( title => 'title', body => 'markup', priority => 5, ... ); $response = $client->feed->publish_action_of_user( title => 'title', body => 'markup', priority => 7, ... );
- friends
-
friends namespace of the API (See WWW::Facebook::API::Friends). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->friends->get; $response = $client->friends->get_app_users; $response = $client->friends->are_friends( uids1 => [1,5,8], uids2 => [2,3,4] );
- groups
-
groups namespace of the API (See WWW::Facebook::API::Groups). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->groups->get( uid => 234324, gids => [2423,334] ); $response = $client->groups->get_members( gid => 32 );
- notifications
-
notifications namespace of the API (See WWW::Facebook::API::Notifications). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->notifications->get; $response = $client->notifications->send( to_ids => [ 1, 3 ], markup => 'markup', no_email => 1, ); $response = $client->notifications->send_request( to_ids => [ 1, 2 ], type => 'event', content => 'markup', image => 'image url', invite => 0|1, );
- photos
-
photos namespace of the API (See WWW::Facebook::API::Photos). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->photos->add_tag( pid => 2, tag_uid => 3, tag_text => "me", x => 5, y => 6 ); $response = $client->photos->create_album( name => 'fun in the sun', location => 'California', description => "Summer '07", ); $response = $client->photos->get( aid => 2, pids => [4,7,8] ); $response = $client->photos->get_albums( uid => 1, pids => [3,5] ); $response = $client->photos->get_tags( pids => [4,5] ); $response = $client->photos->upload( aid => 5, caption => 'beach', data => 'raw data', );
- profile
-
profile namespace of the API (See WWW::Facebook::API::Profile). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->profile->get_fbml( uid => 3 ); $response = $client->profile->set_fbml( uid => 5, markup => 'markup' );
- users
-
users namespace of the API (See WWW::Facebook::API::Users). All method names from the Facebook API are lower_cased instead of CamelCase:
$response = $client->users->get_info( uids => [12,453,67], fields => ['quotes','activities','books'] );
ATTRIBUTE METHODS
These are methods to get/set the object's attributes.
- api_key( $new_api_key )
-
The developer's API key. If
$ENV{'WFA_API_KEY'}
is set, all instances will be initialized with its value. See the Facebook API documentation for more information. - api_version( $new_version )
-
Which version to use (default is "1.0", which is the latest one supported currently). Corresponds to the argument
v
that is passed in to methods as a parameter. - app_id()
-
The application id where your Facebook app is described, e.g.:
http://www.facebook.com/apps/application.php?id=THIS_NUMBER
Remember,
WWW::Facebook::API
is not that clairvoyant: You must first set this number (when callingnew()
) in order to use it. - app_path()
-
If using the Facebook canvas, the path to your application. For example if your application is at http://apps.facebook.com/example/ this should be
"example"
. - apps_uri()
-
The apps uri for Facebook apps. The default is http://apps.facebook.com/.
- callback( $new_default_callback )
-
The callback URL for your application. See the Facebook API documentation. Just a convenient place holder for the value.
- call_success( $is_success, $error_message )
-
Takes in two values, the first setting the object's last_call_success attribute, and the second setting the object's last_error attribute. Returns an array reference containing the last_call_success and last_error values, in that order:
my $response = $client->call_success( 1, undef ); if ( $response->[0] == 1 ) { print 'Last call successful'; } if ( not defined $response->[1] ) { print 'Error message is undefined'; } $client->call_success( 0,'2: The service is not available at this time.'); $response = $client->call_success; if ( not $response->[0] ) { print 'Last call unsuccessful'; } if ( not defined $response->[1] ) { print "Error $response->[1]"; }
The
call
method calls this method, and shouldn't need to be called to set anything, just to get the value later ifthrow_errors
is false. - config($filename)
-
Used when instantiating a new object to set the environment variables. The file has a simple, BASH-style format:
WFA_API_KEY_MYAPP=383378efa485934bc WFA_SECRET_MYAPP=234234ac902f340923 WFA_SESSION_KEY_MYAPP=34589349abce989d WFA_DESKTOP_MYAPP=1
If the file is found, and the environment variables are already set, then the variables will not be changed.
- debug(0|1)
-
A boolean set to either true or false, determining if debugging messages should be carped for REST calls. Defaults to 0.
- desktop(0|1)
-
A boolean signifying if the client is being used for a desktop application. If
$ENV{'WFA_DESKTOP'}
is set, all instances will be initialized with its value. Defaults to 0 otherwise. See the Facebook API documentation for more information. - format('JSON'|'XML')
-
The default format to use if none is supplied with an API method call. Currently available options are XML and JSON. Defaults to JSON.
- last_call_success(1|0)
-
A boolean set to true or false, to show whether the last call was succesful or not. Called by
call_success
. Defaults to 1. - last_error( $error_message )
-
A string holding the error message of the last failed call to the REST server. Called by
call_success
. Defaults to undef. - next( $new_default_next_url )
-
See the Facebook API documentation's Authentication Guide. Just a convenient place holder for the value.
- parse(1|0)
-
Defaults to 1. If set to true, the response returned by each method call will be a Perl structure (see each method for the structure it will return). If it is set to 0, the response string from the server will be returned. (The response string is unescaped if the 'desktop' attribute is false).
- popup( $popup )
-
See the Facebook API documentation's Authentication Guide. Just a convenient place holder for the value.
- query( $query )
-
Stores the current query object to use (either CGI or Apache::Request) but really anything that implements the
param()
method can be used. N.B. When usingrequire_*
methods below, Apache::Request will croak because it does not implement a redirect method. - secret( $new_secret_key )
-
For a desktop application, this is the secret that is used for calling
auth->create_token
andauth->get_session
. For a web application, secret is used for all calls to the API. If$ENV{'WFA_SECRET'}
is set, all instances will be initialized with its value. See the Facebook API documentation under Authentication for more information. - server_uri( $new_server_uri )
-
The server uri to access the Facebook REST server. Default is
'http://api.facebook.com/restserver.php'
. Used to make calls to the Facebook server, and useful for testing. See the Facebook API documentation. - session_expires( $new_expires )
-
The session expire timestamp for the client's user. Automatically set when
$client->auth->get_session
is called. See the Facebook API documentation. - session_key( $new_key )
-
The session key for the client's user. Automatically set when
$client->auth->get_session
is called. See the Facebook API documentation. - session_uid( $new_uid )
-
The session's uid for the client's user. Automatically set when
$client->auth->get_session
is called. See the Facebook API documentation. -
See the Facebook API documentation's Authentication Guide. Just a convenient place holder for the value.
- throw_errors(0|1)
-
A boolean set to either true of false, signifying whether or not to
confess
when an error is returned from the REST server. - ua
-
The LWP::UserAgent agent used to communicate with the REST server. The agent_alias is initially set to "Perl-WWW-Facebook-API/0.4.6".
PUBLIC METHODS
- call( $method, %args )
-
The method which other submodules within WWW::Facebook::API use to call the Facebook REST interface. It takes in a string signifying the method to be called (e.g., 'auth.getSession'), and key/value pairs for the parameters to use: $client->call( 'auth.getSession', auth_token => 'b3324235e' );
For all calls, if
parse
is set to true and an empty hash/array reference is returned from facebook, nothing will be returned instead of the empty hash/array reference. - generate_sig( params => $params_hashref, secret => $secret )
-
Generates a sig when given a parameters hash reference and a secret key.
- get_add_url( %params )
-
Returns the URL to add your application with the parameters (that are given) included. Note that the API key and the API version parameters are also included automatically. If the
next
parameter is passed in, it's string-escaped. Used for platform applications:$response = $client->get_add_url( next => 'http://my.website.com' ); # prints http://www.facebook.com/app.php?api_key=key&v=1.0 # &next=http%3A%2F%2Fmy.website.com print $response;
- get_app_url
-
Returns the URL to your application, if using the Facebook canvas. Uses <$client->app_path>, which you have to set yourself (See <app_path> below).
- get_facebook_url( $subdomain )
-
Returns the URL to Facebook. You can specifiy a specific network as a parameter:
$response = $client->get_facebook_url( 'apps' ); print $response; # prints http://apps.facebook.com
- get_infinite_session_url()
-
Returns the URL for the user to generate an infinite session for your application:
$response = $client->get_infinite_session_url; # prints http://www.facebook.com/codegen.php?api_key=key&v=1.0 print $response;
From what I've seen, the session keys that Facebook returns don't expire automatically, so as long as you don't call $client->auth->logout, you shouldn't even need to worry about this.
- get_login_url( %params )
-
Returns the URL to login to your application with the parameters (that are defined) included. If the
next
parameter is passed in, it's string-escaped:$response = $client->get_login_url( next => 'http://my.website.com' ); # prints http://www.facebook.com/login.php?api_key=key&v=1.0 # &next=http%3A%2F%2Fmy.website.com print $response;
- get_url( $type, @args )
-
Called by all the above
get_*_url
methods above.$type
can be'login'
,'app'
,'add'
,'facebook'
,'infinite_session'
, or'custom'
.@args
contains the query parameters for the the cases when$type
is not'app'
or'facebook'
. In the case of'custom'
, the first item in@args
is the url path relative to the facebook website. All of theget_*_url
methods correspond to the ones in the official PHP client. - log_string($params_hashref, $response)
-
Pass in the params and the response from a call, and it will make a formatted string out of it showing the parameters used, and the response received.
- redirect( $url, $query_object )
-
Called by
require()
to redirect the user either within the canvas or without. If no <$query_object> is defined, then whatever is in$client->query
will be used. (See WWW::Facebook::API::Canvas) If no redirect is required, nothing is returned. That is the only case when there is no return value. If a redirect is required, there are two cases that are covered:- user not logged in
-
If there isn't a user logged in to Facebook's system, then a redirect to the Facebook login page is printed to STDOUT with a next parameter to the appropriate page. The redirect is called with the the CGI module that comes standard with perl. The return value in this case is 1.
- user logged in
-
If the user is logged in to Facebook, and a redirect is required, the necessary FBML is returned:
<fb:redirect url="WHATEVER">
. So the return value is the FBML, which you can then print to STDOUT.
- require_add( $query )
-
Redirects the user to what
get_add_url()
returns. Seerequire()
below for the$query
parameter. - require_frame( $query )
-
Redirects the user to what
get_login_url( canvas =
'1' )> returns. Seerequire()
below for the$query
parameter. - require_login( $query )
-
Redirects the user to what
get_login_url()
returns. Seerequire()
below for the$query
parameter. - require( $what, $query )
-
The official PHP client has
require_*
methods that take no arguments. Logically, you better know what you want to require when you call each of them, so this API consolidates them into one method. The valid values for$what
are'add'
,'frame'
, and'login'
.$query
is the query object to use (most likely CGI). If$query
is undefined, the value of$client-
query >> is used. - session( uid => $uid, key => $session_key, expires => $session_expires )
-
Sets the
user
,session_key
, andsession_expires
all at once. - unescape_string($escaped_string)
-
Returns its parameter with all the escape sequences unescaped. If you're using a web app, this is done automatically to the response.
- verify_sig( sig => $expected_sig, params => $params_hashref )
-
Checks the signature for a given set of parameters against an expected value.
PRIVATE METHODS
- _add_url_params( %params )
-
Called by both
get_login_url
andget_add_url
to process any of their parameters. Prepends the api_key and the version number as parameters and returns the parameter string. - _check_values_of($params_hashref)
-
Makes sure all the values of the
$params_hashref
that need to be set are set. Uses the defaults for those values that are needed and not supplied. - _format_and_check_params( $method, %args )
-
Format method parameters (given in
%args
) according to Facebook API specification. Returns a list of items: A hash reference of the newly formatted params (based on%params
) and the image data if the call is an photo upload:($params, $img_data) = $self->_format_and_check_params( $method, %args );
- _has_error_response( $response )
-
Determines if the response is an error, and logs it appropriately. Returns true if response is an error, false otherwise.
- is_empty_response( $response )
-
Determines if the response is an empty hash or array reference. Returns true if the response is empty, false otherwise.
- _post_request( $params_hashref, $sig, $img_data )
-
Used by
call
to post the request to the REST server and return the response.$img_data
is used when uploading an photo to Facebook. - _parse($string)
-
Parses the response from a call to the Facebook server to make it a Perl data structure, and returns the result.
- _parser()
-
Returns a new instance of JSON::Any.
- _reformat_response( $params, $response )
-
Reformats the response according to whether the app is a desktop app, if the response should be parsed (i.e., changed to a Perlish structure), if the response is empty, etc. Returns the reformatted response.
DIAGNOSTICS
Unable to load JSON module for parsing: %s
-
JSON::Any was not able to load one of the JSON modules it uses to parse JSON. Please make sure you have one (of the several) JSON modules it can use installed.
Error during REST call: %s
-
This means that there's most likely an error in the server you are using to communicate to the Facebook REST server. Look at the traceback to determine why an error was thrown. Double-check that
server_uri
is set to the right location. Cannot create namespace %s: %s
-
Cannot create the needed subclass method. Contact the developer to report.
Cannot create attribute %s: %s
-
Cannot create the needed attribute method. Contact the developer to report.
<_format_and_check_params must be called in list context!
>-
You're using a privat method call and you're not calling it in list context. It returns a list of items, all of which should be interesting to you.
CONFIGURATION AND ENVIRONMENT
WWW::Facebook::API requires no configuration files or environment variables.
DEPENDENCIES
version Crypt::SSLeay Digest::MD5 JSON::Any Time::HiRes LWP::UserAgent
INCOMPATIBILITIES
None.
BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests to bug-www-facebook-api@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
SOURCE REPOSITORY
http://code.google.com/p/perl-www-facebook-api/
TESTING
There are some live tests included, but they are only run if the following environment variables are set: WFA_API_KEY_TEST WFA_SECRET_TEST WFA_SESSION_KEY_TEST
Additionally, if your app is a desktop one, you must set WFA_DESKTOP_TEST
. Also, the session key must be valid for the API key being used.
With live tests enabled, here is the current test coverage:
---------------------------- ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
---------------------------- ------ ------ ------ ------ ------ ------ ------
blib/lib/WWW/Facebook/API.pm 97.3 82.8 63.6 97.8 100.0 7.0 92.2
.../WWW/Facebook/API/Auth.pm 95.1 77.3 100.0 90.9 100.0 92.6 91.3
...WW/Facebook/API/Canvas.pm 97.6 87.5 100.0 100.0 100.0 0.2 97.1
...WW/Facebook/API/Events.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
.../WWW/Facebook/API/FBML.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
...b/WWW/Facebook/API/FQL.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
.../WWW/Facebook/API/Feed.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
...W/Facebook/API/Friends.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
...WW/Facebook/API/Groups.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
...book/API/Notifications.pm 86.7 n/a n/a 71.4 100.0 0.0 84.0
...WW/Facebook/API/Photos.pm 100.0 n/a n/a 100.0 100.0 0.0 100.0
...W/Facebook/API/Profile.pm 87.5 n/a n/a 75.0 100.0 0.0 85.7
...WWW/Facebook/API/Users.pm 92.9 n/a n/a 83.3 100.0 0.0 90.9
Total 97.0 82.4 68.3 95.5 100.0 100.0 93.0
---------------------------- ------ ------ ------ ------ ------ ------ ------
AUTHOR
David Romano <unobe@cpan.org>
CONTRIBUTORS
Clayton Scott http://www.matrix.ca
David Leadbeater http://dgl.cx
Gisle Aas none
J. Shirley <jshirley@gmail.com>
Jim Spath <jspath@gmail.com>
Matt Sickler <imMute@mail.msk3.ath.cx>
Nick Gerakines <nick@socklabs.com>
Olaf Alders <olaf@wundersolutions.com>
Patrick Michael Kane <pmk@wawd.com>
Sean O'Rourke <seano@cpan.org>
Shawn Van Ittersum none
Simon Cavalletto <simonm@cavalletto.org>
Skyler Clark none
Thomas Sibley <tsibley@cpan.org>
LICENSE AND COPYRIGHT
Copyright (c) 2007, David Romano <unobe@cpan.org>
. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.