Sign In with FB issue: FB popup window with the Login dialog is blocked by a web browser

删除回忆录丶 提交于 2019-12-13 05:03:58

问题


FB documentation says:

As noted in the reference docs for this function, it results in a popup window showing the Login dialog, and therefore should only be invoked as a result of someone clicking an HTML button (so that the popup isn't blocked by browsers).

And I did as it says, I put the FB.login function into an onCLick function. But the the Login dialog is still blocked. Why? How to reorder the code?

// Facebook
// Here is a click event, so FB.login is inside a click function.
$("#login_btn_fb").on("click", function() {


    function getUserData(res, fCallback) {
        if(res.authResponse != null) {
            FB.api('/me', function(response) {
                console.log('/me_: ', response);
            });
        }
        else {
            console.log("getUserData CANCEL: ", res);
            return;
        }
    };


    FB.getLoginStatus(function(res) {

        var uid = null;
        var accessToken = null;

        if($.isPlainObject(res)) {

            // Fb+ App+
            if(res.status == "connected") {
                console.error("connected");
                uid = res.authResponse.userID;
                accessToken = res.authResponse.accessToken;
                getUserData(res, null);
            }
            // Fb+ App-
            else if(res.status == "not_authorized") {
                console.error("not_authorized");
                FB.login(function(res) {
                    getUserData(res, null);
                }, {scope: 'email,user_birthday,user_photos,public_profile,user_location'});
            }
            // Fb- App-
            else {
                console.log("UNKNOWN");
                FB.login(function(res) {
                    // console.log("===UNK FB.login res: ", res);
                    getUserData(res, null);
                }, {scope: 'email,user_birthday,user_photos,public_profile,user_location', return_scopes: true});
            };
        }
        // ERROR with FB
        else {
            alert("Facebook sign in failure. Please try again later.");
            return;
        }
    });
});

回答1:


You did not put FB.login in a function that is directly called by a User event, but in an asynchronous callback function of FB.getLoginStatus.

Use FB.getLoginStatus to refresh a User Session and to check if the User is logged in right when you load your Page (right after FB.init), and FB.login only on User interaction - but NOT in an asynchronous callback, of course.



来源:https://stackoverflow.com/questions/25805678/sign-in-with-fb-issue-fb-popup-window-with-the-login-dialog-is-blocked-by-a-web

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