如何使用Perl的WWW :: Facebook :: API发布给用户的新闻源? [英] How do I use Perl's WWW::Facebook::API to publish to a user's newsfeed?

查看:122
本文介绍了如何使用Perl的WWW :: Facebook :: API发布给用户的新闻源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们的网站上使用Facebook Connect与 WWW :: Facebook :: API CPAN模块在用户请求时向我们的用户发布新闻源。

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|,
);

但是,当我们尝试更新上述代码时,我们可以发布包含附件和操作的新闻源故事 Stream.Publish 的Facebook API文档中指定的链接,我们尝试了大约100种不同的方法,没有任何成功。

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.

根据 CPAN文档我们应该做的是将我们的代码更新为跟随并通过附件适当的动作链接似乎不起作用:

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'}"];

上面,也没有任何其他表示,我们尝试似乎工作。我希望一些其他Perl开发人员有这样的工作,并且可以解释如何在Perl中适当地创建附件和action_links变量,以通过WWW :: Facebook :: API发布到Facebook新闻Feed。

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.

提前感谢您的帮助!

推荐答案

我认为问题是您的JSON字符串可能无效只需使用 JSON :: Any 将序列化为Perl即可使其工作数据结构,而不是手动构建JSON字符串。 ( WWW :: Facebook :: API 在引擎盖下使用JSON :: Any;这将是很好的,如果它可以采取一个Perl数据结构而不是一个JSON字符串,我会尝试在这个周末提交一个补丁。)

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/' 
      } ] ) 
);

结果:

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

这篇关于如何使用Perl的WWW :: Facebook :: API发布给用户的新闻源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆