drxm5014 2014-11-29 11:41
浏览 56
已采纳

Facebook Graph API返回空数据

Hello I'm trying to get the FB feed from a user profile on my webpage: I'm using an app and the following is the code that I have tried:

<?php
//function to retrieve posts from facebook’s server

$token = 'https://graph.facebook.com/oauth/access_token?client_id=xxxxxx&client_secret=zzzyyyxxx&grant_type=client_credentials';
$token = file_get_contents($token);
function loadFB($fbID){
    $url = "https://graph.facebook.com/".$fbID."/feed?limit=10";
    $url.= '&access_token=xxxxxx|TLd1z0EZNH1SD1VfA';// *
    $c = curl_init($url);
     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
     $page = json_decode(curl_exec($c));
     var_dump($page);
     curl_close($c);
    if(isset($page->data)){
     return $page->data;}else{echo "aabrakadaabra";}
}

 $fbid = "myfbid";
 date_default_timezone_set("America/Chicago");

 $myPosts = loadFB($fbid);

 foreach($myPosts as $dPost){
    if($dPost->from->id==$fbid){
         $dTime = strtotime($dPost->created_time);
         $myTime=date("M d Y h:ia",$dTime);
        ?>
        <ul>
            <li><?php echo($dPost->message) . $myTime; ?></li>
        </ul>
        <?php
    }
 }
?>

Which is working fine, since I'm getting no error and to confirm the above I tried to see the feeds directly using the url below and it's returning empty data.

https://graph.facebook.com/myID/feed?&access_token=myToken

This is returning:

{
 "data": [

  ]
}

I have tried several other answers posted on SO, but non helped.

  • 写回答

2条回答 默认 最新

  • douxian9943 2014-11-29 12:20
    关注

    User profiles are not really made for using the feed on an external Website, but if you really want to do that you will have to use a User Access Token to access it. Authorize with the read_stream permission to get a User Access Token.

    IMPORTANT: User Tokens are only valid for 60 days, after that you will have to refresh it. And you can´t do that automatically on your server.

    Information about Authorization: https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.2

    You can only access a User feed with a User token of that specific User.

    Better solution: User a Page, you don´t need to authorize for that, you only need an App Access Token.

    More information about Tokens:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧