duanhuchang5056 2014-01-13 23: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-13 23: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>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部