FB.getLoginStatus does not fires callback function

杀马特。学长 韩版系。学妹 提交于 2019-12-03 11:31:01

FB.getLoginStatus does not fire the callback when you are running the website on a different domain than the one that you registered the app with. I usually find myself in this situation when I am developing locally or on a staging server.

For example, if you registered the site with example.com and your staging server is example.mystagingserver.com, the callback wont fire. In this case, you need to create a second application in Facebook and use the Application ID and Secret for the new app.

I just had the same problem, though it only happened to some users.

I finally found out that if your app is sandbox mode, none-developer users can still see your app as a pagetab. But calling getLoginStatus will fail silently (even logging turned on).

Took a while to figure that one out, I hope this can save someone else some time.

I'm using this code, successfully. I'm not quite sure where the differences are.. but I'm using the ASYNC FB Loader.

 window.fbAsyncInit = function() {
   FB.init({ appId: 'XXXXXX', //change the appId to your appId
             status: true, 
             cookie: true,
             xfbml: true,
             oauth: true});

 function authEvent(response) {

   if (response.authResponse) {
          //user is already logged in and connected
           FB.api('/me', function(info) {
              login(response, info);
            });
    } else {
          //user is not connected to your app or logged out
           button.onclick = function() {
            FB.login(function(response) {
            if (response.authResponse) {
               FB.api('/me', function(info) {
                   login(response, info);
              });   
                } else {
               //user cancelled login or did not grant authorization
             }
         }, {scope:'email,rsvp_event,status_update,publish_stream,user_about_me'});     
                    }
                }
            }

            // run once with current status and whenever the status changes
            FB.getLoginStatus(updateButton);
            FB.Event.subscribe('auth.statusChange', updateButton);  
        };
        (function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol 
                + '//connect.facebook.net/en_US/all.js';
            document.getElementById('fb-root').appendChild(e);
        }());


        function login(response, info){
            if (response.authResponse) {
                accessToken =   response.authResponse.accessToken;
        userid = info.id;


                userInfo.innerHTML  = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name+"<br /> Your Access Token: " + accessToken;
            }
        }

You can use the following code to check if the user is logged in:

FB.getLoginStatus(function(response) {
    if (response.authResponse) {
        // logged in and connected user, someone you know
    } else {
        // no user session available, someone you dont know
    }
});

From FB JS SDK Documentation.

You can wrap the whole code in jQuery ready :

$('document').ready(function(){
    ... above code
})

Also you may want to check this question StackOverflow.

I had the same problem. I was working on the facebook login process of our website. During development the "FB.getLoginStatus" did not return a response. I fixed it in the settings of the app on facebook:

-In facebook go to "manage apps"

-Go to the "facebook login" settings of your app

-Add your development url (for example "https://localhost") to the "Valid OAuth Redirect URIs"

(Don't forget to remove the "https://localhost" from the OAuth Redirect URIs when you are finished with developping.)

For me after extensive testing can confirm the dialog to log the user will not show unless you use a valid Application ID

These can be found in

https://developers.facebook.com/apps/{{Application__Id}}/settings/

Just make sure you call the api with the correct ID.

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