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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)