访问facebook好友更新


Access facebook friends updates

如何获得通过facebook api查看好友更新的权限?开发wiki并没有真正帮助我进一步…我只需要一个代码示例…

您需要用户的read_stream扩展权限。http://developers.facebook.com/docs/authentication/permissions/

如果你在一个活跃的用户会话中这样做(例如,不利用offline_access扩展权限),那么读取更新就变成了一个相当微不足道的任务。

示例(使用php):

<?php
require 'facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET',
    'cookie' => true,
));
try
{
    $user_feed = $facebook->api('/me/home/');
    /**
     * You now have the users's news feed, you can do with it what you want.
     * If you want to prune it for friends only...you need to do a little more
     * work..
    **/
    $friend_only_feed = array();
    if (!empty($user_feed['data']))
    {
        $user_feed_pagination = $user_feed['paging'];
        $user_feed = $user_feed['data'];
        $friends = $facebook->api('/me/friends', 'GET');
        $friend_list = array();
        if (!empty($friends['data']))
        {
            $friends = $friends['data'];

            foreach ($friends as $friend)
            {
                $friend_list []= $friend['id'];
            }
        }
        $friend_only_feed = array();
        foreach ($user_feed as $story)
        {
            if (in_array($story['from']['id'], $friend_list))
            {
                $friend_only_feed []= $story;
            }
        }
    }
}
catch (FacebookApiException $e)
{
    /**
     * you don't have an active user session or required permissions 
     * for this user, so rdr to facebook to login.
    **/
    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream'
    ));
    header('Location: ' . $loginUrl);
    exit;
}

print_r($friend_only_feed);

这应该包括获取用户的新闻feed,以及从他们的朋友那里获取他们的所有帖子(不包括页面更新)。如果你没有访问权限,它会将用户重定向到login并给你访问权限。

还值得注意的是,默认的home端点只返回最后25个故事。如果你需要返回到更远的地方,facebook响应上的paging键可以让你做多个请求来返回到更远的地方,或者你可以传递一个数组给api()方法告诉facebook你想要一个更大的限制。

<?php
$user_feed = $facebook->api('/me/home/', 'GET', array(
    'limit' => 500
));