使用Facebook帐户登录我的网站

时间:2013-05-30 06:43:22

标签: email facebook-javascript-sdk facebook-login

我正在尝试将facebook login button合并到我网站的主页中。我已将其与应用相关联,因此如果用户已注册,则我有app idsecret

我使用了来自facebook的JavaScript SDK但是当我按下facebook的登录按钮时,在授权之后,没有任何反应,我的网站的主页再次显示。有谁知道如何从Javascript SDK获取user's email?我想在登录后重定向到另一个页面,但登录后它只停留在该页面上。

<div id="fb-root"></div>
 <script>

   window.fbAsyncInit = function() 
   {

       FB.init({
          appId      : '178124332351909', // App ID
          channelUrl : 'channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
       });

       FB.Event.subscribe('auth.authResponseChange', function(response) 
       {
           if (response.status === 'connected') 
           {
               alert("connected");
               testAPI();
           } 
           else if (response.status === 'not_authorized') 
           {
               alert("nt connected");
               FB.login();
           } 
           else 
           {
               alert("else");
               FB.login();
           }
      });
    };

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

     // Here we run a very simple test of the Graph API after login is successful. 
     // This testAPI() function is only called in those cases. 
     function testAPI() 
     {
        alert("in testapi");
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) 
        {
            console.log('Good to see you, ' + response.name + '.');
        });
     }

 </script>

参考:Add the login code

请帮助您重定向到另一个页面。

2 个答案:

答案 0 :(得分:0)

  1. 要获得email的许可,请将Fb.login()替换为:

    FB.login(function(response)
    {
        if(response.status == 'connected'){
            alert('I am connected')
        }
    },{scope: 'email'});
    
  2. 要在成功登录后将用户重定向到其他页面,只需在testAPI()函数中写入重定向代码:

    function testAPI()
    {
        alert("in testapi");
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) 
        {
           console.log('Good to see you, ' + response.name + '.');
        });
        window.location="http://www.newlocation.com";  // HERE
    }
    

答案 1 :(得分:0)

可能有以下原因:

  • 当您点击登录按钮&gt;&gt;它试图打开弹出窗口&gt;&gt;如果“允许弹出窗口被阻止”&gt;&gt;那么它只会刷新页面,用户会认为什么也没发生。

如果您想让用户收到电子邮件,那么您可以将其作为“perms”参数传递给您。

<div class="fb-login-button" onclick="fbLoginClick();" perms="email"><div>