Facebook getUser() returns 0 [duplicate]

2019-02-22 04:43发布

问题:

This question already has an answer here:

  • Codeigniter, Facebook javascript SDK, PHP SDK Redirect after facebook login doesn't getUser() until refresh 1 answer

I am implementing Facebook login using the PHP SDK with Codeigniter. I get the Facebook login popup, hit login, but I am unable to retrieve the user's id - it always returns 0.

I have put the facebook.php and base_facebook.php files in my libraries file and included it as a library in Codeigniter following a tutorial here.

Can anyone tell me what they think is going on? I am using the following code -

View - Facebook login button

  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '<?php echo $this->config->item('facebook_app_id'); ?>',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true,
      });

          FB.Event.subscribe('auth.login', function() {
            window.location='<?php echo base_url(); ?>auth_other/fb_signin/';
          });

    };
    (function(d){
       var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
       js = d.createElement('script'); js.id = id; js.async = true;
       js.src = "//connect.facebook.net/en_US/all.js";
       d.getElementsByTagName('head')[0].appendChild(js);
     }(document));

  </script>
  <div class="fb-login-button">Login with Facebook</div>

Controller - auth_other/fb_signin

    function fb_signin()
    {           
        $fb_user = null;

        $fb_config = array(
        'appId'  => $this->config->item('facebook_app_id'),
        'secret' => $this->config->item('facebook_app_secret')
        );

        $this->load->library('facebook',$fb_config);

        $fb_user = $this->facebook->getUser();


        echo $fb_user;

回答1:

I was stuck up with similar issue, here is the solution that may help you.

$facebook = new Facebook(array(
  'appId'  => 'xxxx',
  'secret' => 'xxxxx',
  "redirect_uri" => "set the uri to point to the same page"
 ));

$user = $facebook->getUser();

if($user)
{
//your task
} else {
header('location:'. $facebook->getLoginUrl());

}

$user = $facebook->getUser();

if($user)
{
//your task
} else {
header('location:'. $facebook->getLoginUrl());

}



回答2:

Came across a situation where getUser() was returning 0 when it really shouldn't be, the application was still sandboxed. Turns out the user hadn't accept the request to be developer of the application.



回答3:

I've encountered a similar problem. I used an example project, and couldn't figure out why it keeps returning 0. tried nearly all related links here but none solved it.

The solution was to update Facebook's SDK files in the example project. Simple and easily missed,



回答4:

I had same issue, After long time of trying to figure out what the issue is, I found that you need access token to get user ID. To get access token form the signed_request, user must authorize (to get permissions) you app firstly. I hope this will help



回答5:

If App domain is real domain but site url is localhost, getUser function return 0