Facebook登录弹出窗口被浏览器阻止

时间:2018-01-24 16:11:41

标签: javascript facebook facebook-javascript-sdk

我是javascript的新手,真的不明白怎么做,Facebook登录仍然被任何浏览器中的弹出窗口阻止程序阻止

我将此代码放在文件fbapp1.php

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : 'myappid', // App ID
        channelUrl : '//example.com/fbapp1.php', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });
    // this shouldn't be called directly, but instead should be initiated with a user click event
    FB.login(function(response) {
        if (response.authResponse) {
            // document.write ('Access Token: ' + response.authResponse.accessToken);
            window.location = 'https://example.com/fbapp2.php?access_token=' + response.authResponse.accessToken;
        } else {
            alert ('User cancelled login or did not fully authorize.');
        }
    });
};
// Load the SDK Asynchronously
(function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));
</script>

要打开此文件,我在另一个页面中使用简单的html href代码

<a href="https://example.com/fbapp1.php" target="_blank">"Pulsa Fortune App"</a>

但Facebook登录弹出窗口始终被浏览器阻止。上面的代码已经告诉了这件事,但我不知道它的意思是什么?

  

这不应该直接调用,而是应该启动   用户点击事件

请告诉我我的代码应该如何?

1 个答案:

答案 0 :(得分:1)

不要在异步回调函数中调用FB.login - fbAsyncInit是异步的。您必须在用户交互时调用它,否则浏览器会将其阻止。

示例:

document.getElementById('loginBtn').addEventListener('click', function() {
    //do the login
    FB.login(function(response) {
        if (response.authResponse) {
            //user just authorized your app
            document.getElementById('loginBtn').style.display = 'none';
            getUserData();
        }
    }, {scope: 'email,public_profile', return_scopes: true});
}, false);

来源:http://www.devils-heaven.com/facebook-javascript-sdk-login/