2012-08-05 71 views
0

我正在按如下所示进行Facebook登录。但事件不会触发(FB.Event.subscribe(..),GetFBLoginStatus())。我在本地主机上运行它,并在我的应用程序中指定它,但为此目的没有必要。它看起来像JavaScript没有加载,但facebookbutton呈现。我必须改变什么?弹出窗口要求权限,当我点击“登录”,但没有更多。Loading Facebook javascript

 <html> 
<head> 
    <title>My Facebook Login Page</title> 
</head> 
<body> 

    <div id="fb-root"></div> 
    <script type ="text/javascript"> 
     window.fbAsyncInit = function() { 
      FB.init({ 
       appId: 'some id', // App ID 
       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.login', function (response) { alert('You logged in'); }); 
     }; 
     // 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)); 

     function GetFBLoginStatus() { 
      FB.getLoginStatus(function (response) { 
       console.log(response); 
       if (response.status === 'connected') { 
        var accessToken = response.authResponse.accessToken; 
        alert(accessToken); 
       } else if (response.status === 'not_authorized') { 
        //login function 
        alert('login first'); 
       } else { 
        //login function 
        alert('login first'); 
       } 
      }, true); 
     } 
    </script> 
    <div class="fb-login-button" scope="email,user_checkins,read_mailbox">Login with Facebook</div> 
     //this function is not work -> javascript not loaded? 
    <button onclick = "GetFBLoginStatus();">GetStatus</button> 
</body> 

回答

1

也许问题是本地主机不接受coockies。这发生了很多。必须启用cookie,因为这是FB JS SDK将用户登录信息传递到页面的方式。我通常编辑我的系统/主机通过www.localhost.com访问本地主机。由于浏览器不检测本地主机并写入所有必要的cookie。

而且,最好是定义里面window.fbAsyncInitGetFBLoginStatus()功能,因为它可以在FB.init之前被调用,会崩溃,因为FB对象(你的函数内部使用:FB.getLoginStatus)尚未建立。

我听到有人说,有时auth.login事件不会触发当用户已经授权您的应用程序。这是因为这个事件只在状态CHANGES连接时触发。如果用户过去已经授权,则没有任何改变。还有其他事件,如auth.authResponseChangeauth.statusChange。也许他们更好地满足您的需求。

+0

感谢您的有用评论,Victor.Also我建议开发人员这个http://facebookhelper.codeplex.com/ – 2012-08-06 10:05:07

+0

在mvc中使用此包添加参考webmatrix.data && webmatrix.webdata并在属性中设置锁定'真正' – 2012-08-06 14:15:14