2011-04-27 84 views
2

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

回答

2

您需要为您的用户提供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); 

这应该讨论如何获取用户的新闻源,并从他们的朋友(不包括网页更新)获得他们的所有帖子。如果您没有访问权限,它将重定向用户登录并授予您访问权限。

还值得注意的是,默认的home端点只能让您回到最近的25个故事。如果你需要返回更远的位置,那么facebook响应中的paging键可以让您执行多个请求以返回更远,或者您可以将数组传递给api()方法,告诉Facebook您想要更大的限制。

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

这工作,但如果我想状态更新也是,甚至posible? – DaDu 2011-04-28 12:10:49

+0

@DaDu状态更新被定义为用户发布的与其没有任何附件的任何内容。这意味着,用户发布的任何内容不是链接,视频,照片等。该图实际上使用每个元素的“类型”键来标识每个元素在/ home终点上的发布类型。希望有所帮助! – 2011-04-28 18:54:36