Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Perl's WWW::Facebook::API to publish to a user's newsfeed?

Tags:

facebook

perl

We use Facebook Connect on our site in conjunction with the WWW::Facebook::API CPAN module to publish to our users newsfeed when requested by the user.

So far we've been able to successfully update the user's status using the following code:

use WWW::Facebook::API;
my $facebook = WWW::Facebook::API->new(
    desktop => 0,
    api_key => $fb_api_key,
    secret => $fb_secret,
 session_key => $query->cookie($fb_api_key.'_session_key'),
 session_expires => $query->cookie($fb_api_key.'_expires'),
 session_uid => $query->cookie($fb_api_key.'_user')
);

my $response = $facebook->stream->publish(
 message => qq|Test status message|,
);

However, when we try to update the code above so we can publish newsfeed stories that include attachments and action links as specified in the Facebook API documentation for Stream.Publish, we have tried about 100 different ways without any success.

According to the CPAN documentation all we should have to do is update our code to something like the following and pass the attachments & action links appropriately which doesn't seem to work:

my $response = $facebook->stream->publish(
 message => qq|Test status message|,
    attachment => $json,
    action_links => [@links],
);

For example, we are passing the above arguments as follows:

$json = qq|{ 'name': 'i\'m bursting with joy', 'href': ' http://bit.ly/187gO1', 'caption': '{*actor*} rated the lolcat 5 stars', 'description': 'a funny looking cat', 'properties': { 'category': { 'text': 'humor', 'href': 'http://bit.ly/KYbaN'}, 'ratings': '5 stars' }, 'media': [{ 'type': 'image', 'src': 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg', 'href': 'http://bit.ly/187gO1'}] }|;
@links = ["{'text':'Link 1', 'href':'http://www.link1.com'}","{'text':'Link 2', 'href':'http://www.link2.com'}"];

The above, nor any of the other representations we tried seem to work. I'm hoping some other perl developer out there has this working and can explain how to create the attachment and action_links variables appropriately in Perl for posting to the Facebook news feed through WWW::Facebook::API.

Thanks in advance for your help!

like image 297
Russell C. Avatar asked Apr 16 '10 17:04

Russell C.


1 Answers

I think the problem is that your JSON string might be invalid. I was able to get it to work by just using JSON::Any to serialize a Perl data structure instead of building the JSON string manually. (WWW::Facebook::API uses JSON::Any under the hood; it would be nice if it could take a Perl data structure instead of a JSON string. I will try to submit a patch this weekend.)

use WWW::Facebook::API;
use JSON::Any;

my $j = JSON::Any->new;

my $fb = WWW::Facebook::API->new( 
    desktop => 0, 
    api_key => $api_key, 
    secret  => $secret, 
    session_key => $session, 
    session_expires => $expires, 
    session_uid => $fb_uid 
);

my $res = $fb->stream->publish( 
    message => 'Test message', 
    attachment => $j->objToJson( 
        { name => 'Foo bar baz', 
          href => 'http://www.google.com/', 
          description => "this is a thing" 
       } ), 
    action_links =>  $j->objToJson( 
      [ { text => 'action link text', 
          href => 'http://www.foobar.com/' 
      } ] ) 
);

The result:

http://www.friedo.com/fb_attach.jpg

like image 144
friedo Avatar answered Sep 29 '22 01:09

friedo