0

我有一个登录页面,我将在其中提供 facebook 登录的链接,但不幸的是,一旦我登陆登录页面,facebook oauth 对话框就会打开。我想要点击链接而不是页面加载。有什么帮助吗?

<p><a onclick='login(); return false;'>Login</a></p>
<script>

    // Additional JS functions here
    window.fbAsyncInit = function() {
        FB.init({
            appId      : '3423243443', // App ID
            status: true, // check login status
            cookie: true, // enable cookies to allow server to access session,
            xfbml: true, // enable XFBML and social plugins
            oauth: true // enable OAuth 2.0
            // channelUrl: 'http://www.yourdomain.com/channel.html' //custom channel
        });
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // connected
                alert("connected1"+response.authResponse.expiresIn);

            } else if (response.status === 'not_authorized') {
                // not_authorized
                login();
            } else {
                // not_logged_in
                alert("not logged in");
                login();
            }
        },true);



    };


    (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));

    /*function login() {
        FB.login(function(response) {
            if (response.authResponse) {
                testAPI(response)
                // connected
                alert("connected2   "+response.authResponse);
            } else {
                // cancelled
                alert("cancelled");
                alert("cancelled"+response.authResponse);
            }
        });
    } ;

    function testAPI() {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.email + '.');
        });
    }  ;*/


</script>
4

2 回答 2

1

因为您在每个页面加载时在 window.fbAsyncInit 中插入了调用登录函数的 FB.getLoginStatus。

您应该仅通过一个按钮触发 FB.getLoginStatus,并使用 onclick 触发器。

这是一个小例子: http: //facebook.stackoverflow.com/a/15720747/2212966

于 2013-03-30T16:30:36.183 回答
0

我用 jquery 按钮单击事件处理程序包装了我的。这是我的代码片段:

window.fbAsyncInit = function() {

      FB.init({
        appId      : 'my-app-id',//use your facebook appId in here
        status     : false,
        cookie     : true,
        xfbml      : true,
        version    : 'v2.2'
      });

      jQuery('#signUpWithFacebookButtonId').click(function(){
          FB.login(function(response) {
              if (response.status === 'connected') {
                console.log('Logged into your app and Facebook.');
              } else if (response.status === 'not_authorized') {
                console.log('The person is logged into Facebook, but not your app.');
              } else {
                console.log('The person is not logged into Facebook, so we are not sure if they are logged into this app or not.');
              }
          }, {scope: 'public_profile, email'});
      });

  };//end window.fbAsyncInit function
于 2016-02-06T18:36:32.447 回答