The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Facebook::Graph::Cookbook::Recipe3 - Impersonation

VERSION

version 1.1205

DESCRIPTION

Building an application that can post as another page under my control.

Assumptions

We're assuming you've already learned the basics of Facebook::Graph through the other recipes and have already set up your application access token.

RECIPE

use Facebook::Graph;
use Ouch;
use Config::JSON;

# init
my $fb_config = Config::JSON->new('/path/to/my.conf')->get('facebook');
my $fb = Facebook::Graph->new($fb_config);

# get list of available my pages and access tokens
my $pages = $fb->query->find('me/accounts')->include_metadata->request->as_hashref->{data};
my $token;

# identify the specific page i want to post to
foreach my $page (@{$pages}) {
    $token = $page->{access_token} if $page->{id} eq $fb_config->{page_id};
}
unless (defined $token) {
    ouch 504, "Couldn't post to Facebook.";
}

# post
$fb->access_token($token);
my $response_id = $fb->add_post
    ->set_message($message);
    ->set_link_name($link_name)
    ->set_link_uri($link_uri)
    ->set_link_description($link_description)
    ->set_picture_uri($picture_uri)
    ->publish
    ->as_hashref
    ->{id};

SEE ALSO

For more recipes, check out the Facebook::Graph::Cookbook.

LEGAL

Facebook::Graph is Copyright 2010 - 2017 Plain Black Corporation (http://www.plainblack.com) and is licensed under the same terms as Perl itself.