简体   繁体   English

授权成功后,Facebook SDK getUser()方法返回0

[英]Facebook SDK getUser() method returns 0 after successful authorization

I recently changed my host and noticed that Facebook login is no longer working. 我最近更改了主机,发现Facebook登录不再起作用。 It is not throwing any errors, but it's simply not working. 它不会引发任何错误,但是根本无法正常工作。

My code: Tested with the given example 我的代码: 使用给定的示例进行了测试

My workflow: 我的工作流程:

  1. User clicks on the login button (getLoginUrl() method). 用户单击登录按钮(getLoginUrl()方法)。
  2. Facebook authorization window pops up 弹出Facebook授权窗口
  3. User accepts it and gets redirected back to my page with ?code=..&state=.. 用户接受它,并使用?code = ..&state = ..将其重定向回到我的页面。
  4. getUser() returns 0, but it should return the users ID getUser()返回0,但应返回用户ID

I tried googling for answers. 我尝试使用Google搜索来寻找答案。 This is what I tried: 这是我尝试的:

  1. To change the getCode() method -> _REQUEST problem 更改getCode()方法-> _REQUEST问题
  2. Turn on/off cookies 开启/关闭Cookie

Is there anything else I can do to fix this problem? 我还能做些什么来解决此问题?

Try following, add the JS code below the page where you are doing authentication 尝试执行以下操作,在执行身份验证的页面下方添加JS代码

<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>

Make sure you add a div with id = "fb-root" within the HTML and closed that, best practice is to add that above your JS code as above. 确保在HTML中添加一个id =“ fb-root”的div并关闭它,最佳实践是如上所述将其添加到您的JS代码上方。

Then use the PHP script to generate the login button and add the following code on the top of your page so that when the page reloads the code gets executed 然后使用PHP脚本生成登录按钮,并在页面顶部添加以下代码,以便在页面重新加载时执行代码

require_once 'src/facebook.php'; //include the facebook php sdk
$facebook = new Facebook(array(
    'appId'  => 'XXXXXXXXXXXXXX',    //app id
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX', // app secret
));

$user_id = $facebook->getUser();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM