Facebook login window appears and disappears very quickly

梦想与她 提交于 2019-12-10 19:07:54

问题


Recently I've uploaded a piece of code to phpfog and I've come across with a problem which didn't happen locally:

When the page loads, it tries to get $idFacebook that is supposed to be set:

$idFacebook = $facebook->getUser();
if ($idFacebook) { ... }

I'm logged to Facebook, however, the condition fails and the button to login appears:

<div class="fb-login-button" data-scope="user_likes,user_photos"></div>

The other problem is that when I press the button, it seems that the login window appears but it closes automatically very quickly.

My code is basically the same that Heroku provides when you create a Facebook app. The repository is this one: https://github.com/heroku/facebook-template-php. I can't find how to solve it and I've seen that lots of people have experienced several problems with facebook login.

I'm looking forward you can help me. Thanks in advance.


回答1:


My actual error was that $facebook->getUser() returned 0 because the app wasn't authenticated and the app wasn't authenticated because I moved my code from a host to another in a different domain and I forgot changing the application configuration in http://developers.facebook.com.

Now, the app gets the login automatically. Thanks everybody!




回答2:


The login window disappearing quickly is Facebook checking whether you need to grant permissions to the app or not. It is closing almost instantly because you are already logged in to and authorised with Facebook.

I'm not familiar with the Heroku / Facebook integration but I would assume the $idFacebook problem is to do with invalid configuration.




回答3:


The login window is dissappearing quickly because you may be logged in to facebook in same browser.

I'm not familiar with Heroku but I've overcome this issue in Zend Framework - php. I've used javascript-sdk for login and logout. You may refer this link.

http://developers.facebook.com/blog/post/2011/07/21/updated-javascript-sdk-and-oauth-2-0-roadmap/

So when you implement this example in your code It will ask you to logout when you logged-in in to FB in same browser. so when you click logout , It will again ask you to log-in and Thats it.

Hope it helps.




回答4:


To avoid that, you have to change the behaviour of your facebook script slightly with the FB.getLoginStatus. It checks if the user is logged already in Facebook and if he already authorised your app.

FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        window.location = //redirect to page with logged user (you have the response token in response)
    } else {
      //Show the login popup
      FB.login(function(response) {
        if (response.authResponse) {
          window.location = //redirect to location after correct login
        }
      }, { scope: '<scopes>', state: '<state>' });
    }
  });

This way, if the user is "connected" (logged in and app authorised), you won't call the FB.login method, so the login window won't flash. In any other case, it will show the login window, which is the expected result.

Check https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus for the complete documentation.



来源:https://stackoverflow.com/questions/11646494/facebook-login-window-appears-and-disappears-very-quickly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!