Facebook-OpenGraph
view release on metacpan or search on metacpan
Revision history for Perl extension Facebook::OpenGraph
## CAUTION ####################################################################
FROM Version 1.22 it doesn't check if 'expires' parameter is properly returned
from /oauth/access_token endpoint. Affected methods are listed below:
- get_user_token_by_code
- get_user_token_by_cookie
- exchange_token
There are some edge cases that they do not return 'expires' so this module does
not check its existense, any more. If 'expires' should be returned on your use
case, you must explicitly check by yourself.
The detailed schenario concerning those edge cases are shown at the link below.
https://developers.facebook.com/bugs/597779113651383/
$fb->publish_action($action_type, +{$object_type => $object_url});
# DESCRIPTION
Facebook::OpenGraph is a Perl interface to handle Facebook's Graph API.
This module is inspired by [Facebook::Graph](https://metacpan.org/pod/Facebook::Graph), but mainly focuses on simplicity
and customizability because we must be able to keep up with frequently changing
API specification.
This module does **NOT** provide ways to set and validate parameters for each
API endpoint like Facebook::Graph does with Any::Moose. Instead it provides
some basic methods for HTTP request. It also provides some handy methods that
wrap `request()` for you to easily utilize most of Graph API's functionalities
including:
- API versioning that was introduced at f8, 2014.
- Acquiring user, app and/or page token and refreshing user token for
long lived one.
- Batch Request
- FQL
- FQL with Multi-Query
- Field Expansion
- Etag
- Wall Posting with Photo or Video
- Creating Test Users
- Checking and Updating Open Graph Object or Web Page with OGP
- Publishing Open Graph Action
- Deleting Open Graph Object
- Posting Staging Resource for Open Graph Object
In most cases you can specify endpoints and request parameters by yourself and
pass them to request() so it should be easier to test latest API specs. Other
requesting methods merely wrap request() method for convinience.
# METHODS
## Class Methods
### `Facebook::OpenGraph->new(\%args)`
Creates and returns a new Facebook::OpenGraph object.
my $fb = Facebook::OpenGraph->new(+{is_beta => 1});
$c->redirect($fb->site_uri($path_to_canvas));
# https://www.beta.facebook.com/$path_to_canvas
### `$fb->parse_signed_request($signed_request_str)`
It parses signed\_request that Facebook Platform gives to you on various
situations. situations may include
- Given as a URL fragment on callback endpoint after login flow is done
with Login Dialog
- POSTed when Page Tab App is loaded
- Set in a form of cookie by JS SDK
Fields in returning value are introduced at
[https://developers.facebook.com/docs/reference/login/signed-request/](https://developers.facebook.com/docs/reference/login/signed-request/).
my $req = Plack::Request->new($env);
my $val = $fb->parse_signed_request($req->query_param('signed_request'));
||= $self->SUPER::get_app_token->{access_token};
}
Or you might want to use [Cache::Memory::Simple](https://metacpan.org/pod/Cache::Memory::Simple) or something similar to
refetch token at an interval of your choice. Maybe you want to store token on
DB and override this method to return the stored value.
### `$fb->get_user_token_by_code($given_code)`
Obtain an access token for user based on `$code`. `$code` should be obtained
on your callback endpoint which is specified on `eredirect_uri`. Give the
returning access token to `set_access_token()` and you can act on behalf of
the user.
FYI: _expires_ or _expires\_in_ is **NOT** returned on some edge cases. The
detail and reproductive scenario should be found at
[https://developers.facebook.com/bugs/597779113651383/](https://developers.facebook.com/bugs/597779113651383/).
# On OAuth callback page which you specified on $fb->redirect_uri.
my $req = Plack::Request->new($env);
my $token_ref = $fb->get_user_token_by_code($req->query_param('code'));
}
],
limit => 5,
}
}
]);
# 'name,email,albums.fields(name,photos.fields(name,picture,tags.limit(2)).limit(3)).limit(5)'
### `$fb->publish_action($action_type, \%param)`
Alias to `request()` that optimizes body content and endpoint to send `POST`
request to publish Open Graph Action.
my $res = $fb->publish_action('give', +{crap => 'https://sample.com/poop/'});
#{id => 123456}
### `$fb->create_test_users(\@settings)`
my $res = $fb->create_test_users([
+{
permissions => [qw/publish_actions/],
lib/Facebook/OpenGraph.pm view on Meta::CPAN
croak 'app_id and secret must be set' unless $self->app_id && $self->secret;
$param_ref = +{
%$param_ref,
client_id => $self->app_id,
client_secret => $self->secret,
};
my $response = $self->request('GET', '/oauth/access_token', $param_ref);
# Document describes as follows:
# "The response you will receive from this endpoint, if successful, is
# access_token={access-token}&expires={seconds-til-expiration}
# If it is not successful, you'll receive an explanatory error message."
#
# It, however, returnes no "expires" parameter on some edge cases.
# e.g. Your app requests manage_pages permission.
# https://developers.facebook.com/bugs/597779113651383/
if ($response->is_api_version_eq_or_later_than('v2.3')) {
# As of v2.3, to be compliant with RFC 6749, response is JSON formatted
# as described below.
# {"access_token": <TOKEN>, "token_type":<TYPE>, "expires_in":<TIME>}
lib/Facebook/OpenGraph.pm view on Meta::CPAN
$fb->publish_action($action_type, +{$object_type => $object_url});
=head1 DESCRIPTION
Facebook::OpenGraph is a Perl interface to handle Facebook's Graph API.
This module is inspired by L<Facebook::Graph>, but mainly focuses on simplicity
and customizability because we must be able to keep up with frequently changing
API specification.
This module does B<NOT> provide ways to set and validate parameters for each
API endpoint like Facebook::Graph does with Any::Moose. Instead it provides
some basic methods for HTTP request. It also provides some handy methods that
wrap C<request()> for you to easily utilize most of Graph API's functionalities
including:
=over 4
=item * API versioning that was introduced at f8, 2014.
=item * Acquiring user, app and/or page token and refreshing user token for
long lived one.
lib/Facebook/OpenGraph.pm view on Meta::CPAN
=item * Checking and Updating Open Graph Object or Web Page with OGP
=item * Publishing Open Graph Action
=item * Deleting Open Graph Object
=item * Posting Staging Resource for Open Graph Object
=back
In most cases you can specify endpoints and request parameters by yourself and
pass them to request() so it should be easier to test latest API specs. Other
requesting methods merely wrap request() method for convinience.
=head1 METHODS
=head2 Class Methods
=head3 C<< Facebook::OpenGraph->new(\%args) >>
Creates and returns a new Facebook::OpenGraph object.
lib/Facebook/OpenGraph.pm view on Meta::CPAN
$c->redirect($fb->site_uri($path_to_canvas));
# https://www.beta.facebook.com/$path_to_canvas
=head3 C<< $fb->parse_signed_request($signed_request_str) >>
It parses signed_request that Facebook Platform gives to you on various
situations. situations may include
=over 4
=item * Given as a URL fragment on callback endpoint after login flow is done
with Login Dialog
=item * POSTed when Page Tab App is loaded
=item * Set in a form of cookie by JS SDK
=back
Fields in returning value are introduced at
L<https://developers.facebook.com/docs/reference/login/signed-request/>.
lib/Facebook/OpenGraph.pm view on Meta::CPAN
||= $self->SUPER::get_app_token->{access_token};
}
Or you might want to use L<Cache::Memory::Simple> or something similar to
refetch token at an interval of your choice. Maybe you want to store token on
DB and override this method to return the stored value.
=head3 C<< $fb->get_user_token_by_code($given_code) >>
Obtain an access token for user based on C<$code>. C<$code> should be obtained
on your callback endpoint which is specified on C<eredirect_uri>. Give the
returning access token to C<set_access_token()> and you can act on behalf of
the user.
FYI: I<expires> or I<expires_in> is B<NOT> returned on some edge cases. The
detail and reproductive scenario should be found at
L<https://developers.facebook.com/bugs/597779113651383/>.
# On OAuth callback page which you specified on $fb->redirect_uri.
my $req = Plack::Request->new($env);
my $token_ref = $fb->get_user_token_by_code($req->query_param('code'));
lib/Facebook/OpenGraph.pm view on Meta::CPAN
}
],
limit => 5,
}
}
]);
# 'name,email,albums.fields(name,photos.fields(name,picture,tags.limit(2)).limit(3)).limit(5)'
=head3 C<< $fb->publish_action($action_type, \%param) >>
Alias to C<request()> that optimizes body content and endpoint to send C<POST>
request to publish Open Graph Action.
my $res = $fb->publish_action('give', +{crap => 'https://sample.com/poop/'});
#{id => 123456}
=head3 C<< $fb->create_test_users(\@settings) >>
my $res = $fb->create_test_users([
+{
permissions => [qw/publish_actions/],
lib/Facebook/OpenGraph/Response.pm view on Meta::CPAN
}
return $err_str;
}
sub as_json {
my $self = shift;
my $content = $self->content;
if ($content =~ m{\A (true|false) \z}xms) {
# On v2.0 and older version, some endpoints return plain text saying
# 'true' or 'false' to indicate result, so make it JSON formatted for
# our convinience. The key is named "success" so its format matches with
# other endpoints that return {"success": "(true|false)"}.
# From v2.1 they always return in form of {"success": "(true|false)"}.
# See https://developers.facebook.com/docs/apps/changelog#v2_1_changes
$content = sprintf('{"success" : "%s"}', $1);
};
return $content; # content is JSON formatted
}
sub as_hashref {
my $self = shift;
( run in 1.080 second using v1.01-cache-2.11-cpan-2b1a40005be )