duanhuchang5056 2014-01-14 07:03
浏览 29
已采纳

php必须使用活动访问令牌进行查询

I am using facebook SDK to get facebook friends list. If I Invoke API with Javascript FB.init(); it works perfectly fine..

but if I use it directly like

facebook = new Facebook(array(
'appId'  => $appid,
'secret' => $secret
));

$fbuser = $facebook->getUser();
try{
    $user_profile = $facebook->api('/me');
    print_r($user_profile);
} catch(Exception $e){
    echo $e->getMessage();
}

(User is logged in with facebook in the same browser) It always gives me an error : An active access token must be used to query information about the current user.

Please help me how to use it without FB.init() or redirect anywhere..

  • 写回答

1条回答 默认 最新

  • dsb12300 2014-01-14 07:38
    关注

    Use this

    facebook = new Facebook(array(
    'appId'  => $appid,
    'secret' => $secret,
    'cookie' => true
    ));
    
    $fbuser = $facebook->getUser();
    if($fbuser){
       $access_token = $facebook->getAccessToken();
       $facebook->setAccessToken($access_token);
       try{
           $user_profile = $facebook->api('/me');
           print_r($user_profile);
       } catch(Exception $e){
          echo $e->getMessage();
       }
    }else{
      // User not logged in generate the login button or link here
    }
    

    Make sure in the above script when user login ffor the first time it reloads the page in other words you need to provide the redirect_uri https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

    Also you can use the following api to get the user details

    $user_profile =  $facebook->api('/'. $fbuser,'GET');
    

    Then add the following below the page. This will check if the user is already logged in it will re-direct to the same page, in other words it will not ask user to login again

    <div id="fb-root"></div>
    
    <script type="text/javascript">
        window.fbAsyncInit = function()
                {
                    FB.init
                    ({
                        appId   : 'your fb app id',
                        status  : true, // check login status
                        cookie  : true, // enable cookies to allow the server to access the session
                        xfbml   : true, // parse XFBML
                        oauth   : true
                    });
                    FB.Event.subscribe('auth.login', function()
                    {
                        window.location.reload();
                    });
                };
    
              (function()
              {
                var e = document.createElement('script');
                e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
                e.async = true;
                document.getElementById('fb-root').appendChild(e);
                }());
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题