FACEBOOK O-AUTH对话框自动打开

时间:2013-03-30 16:25:59

标签: facebook facebook-graph-api facebook-javascript-sdk

我有一个登录页面,我将为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>

2 个答案:

答案 0 :(得分:1)

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

你应该只通过一个带有onclick触发器的按钮来触发FB.getLoginStatus。

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

答案 1 :(得分: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
相关问题