php Facebook getUser() 返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8671079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:14:06  来源:igfitidea点击:

Facebook getUser() returns 0

phpfacebookcodeigniterauthenticationlogin

提问by Tom

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.

我正在使用带有 Codeigniter 的 PHP SDK 实现 Facebook 登录。我得到 Facebook 登录弹出窗口,点击登录,但我无法检索用户的 ID - 它总是返回 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.

我已将 facebook.php 和 base_facebook.php 文件放在我的库文件中,并按照此处的教程将其作为库包含在 Codeigniter 中。

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

谁能告诉我他们是怎么想的?我正在使用以下代码 -

View - Facebook login button

查看 - Facebook 登录按钮

  <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

控制器 - 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;

回答by Arun G

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());

}

}

回答by Rick Haywood

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.

遇到了 getUser() 在不应该返回 0 时返回 0 的情况,应用程序仍处于沙盒状态。结果发现用户没有接受成为应用程序开发人员的请求。

回答by Eran

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.

我遇到过类似的问题。我使用了一个示例项目,但不知道为什么它一直返回 0。在这里尝试了几乎所有相关链接,但没有一个解决它。

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

解决方案是在示例项目中更新 Facebook 的 SDK 文件。简单又容易错过,

回答by Mohammed Israwi

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

我遇到了同样的问题,经过长时间的尝试找出问题所在,我发现您需要访问令牌来获取用户 ID。要从signed_request 中获取访问令牌,用户必须首先授权(以获取权限)您的应用程序。我希望这个能帮上忙

回答by levye

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

如果 App 域是真实域但站点 url 是 localhost,则 getUser 函数返回 0