0

我正在尝试编写一个 facebook 登录按钮。以下是我的 facebook javascript。一切顺利(即弹出窗口出现,facebook 登录提示等),直到我通过 facebook 应用程序成功登录,行“alert('Login Failed!');” 正在执行。这是由于 response.authResponse != 1。有人可以帮我吗?

<div id="fb-root"></div>
<script type="text/javascript">
//<![CDATA[
window.fbAsyncInit = function() {
   FB.init({
     appId      : 'I placed my app id here', // App ID
     channelURL : '', // Channel File, not required so leave empty
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     oauth      : true, // enable OAuth 2.0
     xfbml      : true  // parse XFBML
   });
};
// logs the user in the application and facebook
function fblogin(){
FB.getLoginStatus(function(r){
     if(r.status === 'connected'){
            window.location.href = 'fbconnect.php';
     }else{
        FB.login(function(response) {
                if(response.authResponse) {
              //if (response.perms)
                    alert('Success!');
                    window.location.href = 'fbconnect.php';
            } else {
                alert('Login Failed!');
              // user is not logged in
            }
     },{scope:'email'}); // which data to access from user profile
 }
});
}
// Load the SDK Asynchronously
(function() {
   var e = document.createElement('script'); e.async = true;
   e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';                
   document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>

这是我的登录按钮

echo '<a href=# onclick="fblogin();">Facebook Login</a>';
4

2 回答 2

0

问题解决了。显然问题是我的沙盒模式在我的 facebook 应用程序基本信息中处于打开状态。将其关闭将使上述代码有效。

谢谢您的帮助!

于 2013-07-03T09:01:30.830 回答
0

试试这个,不要用 r.status === 'connected' 测试它

if (r.authResponse) {
    //user is already logged in and connected
    FB.api('/me', function (info) {
        login(response, info);
    });

} 
else {
    //user is not connected to your app or logged out
    FB.login(function (response) {
        if (response.authResponse) {
            FB.api('/me', function (info) {});
        } else {
            //user cancelled login or did not grant authorization

        }
    }, {scope : 'email'});
}
于 2013-07-03T02:14:27.903 回答