Access facebook friends updates
How do I get the permission to see the updates from my friends trough the facebook api? The de开发者_JAVA百科v wiki doesn't really help me further... I just need a code example...
You need the read_stream extended permission for your user. http://developers.facebook.com/docs/authentication/permissions/
If you're doing this during an active user-session (eg, not utilizing the offline_access extended permission) it then becomes a pretty trivial task to read the updates.
Example (using 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);
That should cover getting the users' news feed, and getting all their posts from their friends (excludes pages updates). If you don't have access, it will redirect the user to login and give you access.
It's also worth noting that the default home
endpoint only gives you back the last 25 stories. If you need to go back farther than that, the paging
key on the facebook response lets you do multiple requests to go back farther, or you can pass an array to the api() method telling facebook you want a larger limit.
<?php
$user_feed = $facebook->api('/me/home/', 'GET', array(
'limit' => 500
));
精彩评论