我为创建的许多Facebook应用程序使用了很长时间以下代码,但由于几个小时前,此代码似乎不再起作用。我不知道此代码是否将不再可用或暂时不可用。是否有与以下代码相似的替代代码?

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>FB APP</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<style>
body{
    background: #ffffff url('lp.jpg') top center no-repeat;
    margin-left: 160px;
}
@font-face {
    font-family:fabada;
    src: url('fabada.ttf');
}
#likegate{
    text-align: center;
    margin-top: 400px;
    margin-left: 350px;
    padding-bottom: 100px;
}
</style>
</head>
<body>
<!-- DEFINITING FACEBOOK APP ID -->
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
    FB.init({
        appId   : '448829841800709',
        status  : true, // check login status
        cookie  : true, // enable cookies to allow the server to access the session
        xfbml   : true  // parse XFBML
    });
    FB.UIServer.setActiveNode = function(a,b){FB.UIServer._active[a.id]=b;}
    // Scroll Bar Fixed
    window.setTimeout(function() {
        FB.Canvas.setAutoResize(90);
        FB.Canvas.setAutoResize();
        FB.Canvas.setSize({ width: 810, height: 555 });
    }, 250);
</script>
<script type="text/javascript">
function loging(getuid){
jQuery("#loginfb").hide();
jQuery("#fbload").show();
FB.api(
          {
            method: 'fql.query',
            query: 'SELECT name, uid FROM user WHERE uid=' + getuid
          },
          function(response) {
            var user = response[0];
            jQuery.ajax({
            type: 'GET',
            url: 'login.php?uid=' + getuid,
            success: function(data) {
                jQuery('#jstarget').html(data);
                jQuery("#fbload").hide();
            }
            });
          }
        );
}

window.fbAsyncInit = function () {
FB.init({
          appId   : '448829841800709',
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true, // parse XFBML
          oauth   : true
        });
}

jQuery(document).ready(function() {
    // fetch the status on load
    jQuery('#loginfb').bind('click', function() {
        FB.init({
              appId   : '448829841800709',
              status  : true, // check login status
              cookie  : true, // enable cookies to allow the server to access the session
              xfbml   : true, // parse XFBML
              oauth   : true
            });
        if (response.authResponse){
            FB.api('/me', function(response) {
            loging(response.id);
            });
        }
        else{
            //user is not connected to your app or logged out
            FB.login(function(response) {
                if(response.authResponse) {
                    FB.api('/me', function(response) {
                    loging(response.id);
                    });
                }
                else {
                    //user cancelled login or did not grant authorization
                }
            }, {scope:'publish_stream'});
        }
    });
    // handle a session response from any of the auth related calls
});
</script>
<div id="jstarget"></div>

<div id="box">
    <!--MAIN CONTENT-->
    <div id="content">
        <div id="likegate">
            <img src="login.png" style="cursor: pointer; float: left;" id="loginfb" alt="Login">
            <img src="ajax-loader.gif" style="float: left; padding-left: 50px; display:none" id="fbload" alt="Loading">
        </div>
    </div>
</div>
</body>
</html>


更新

当我删除这个

if (response.authResponse){
    FB.api('/me', function(response) {
    loging(response.id);
    });
}
else{
   // this code here not removed
}


它显示弹出权限对话框,但是当我允许该应用程序时,我再次单击登录按钮,为什么它又显示弹出权限对话框?

最佳答案

您需要删除所有多个FB.init,不要在jquery 1.7.1之后使用.bind(),它将被.on()贬值。

2)添加channel.html,它可以帮助您解决任何XD问题。
3)使用FB.getAuthResponse()获得身份验证响应

我做了快速清理,但从未测试!而且看起来还有很多改进的空间:

<!-- DEFINITING FACEBOOK APP ID -->
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '448829841800709', // App ID
      channelUrl : '//WWW.YOUR_DOMAIN.COM/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.UIServer.setActiveNode = function(a,b){FB.UIServer._active[a.id]=b;}
    // Scroll Bar Fixed
    window.setTimeout(function() {
        FB.Canvas.setAutoResize(90);
        FB.Canvas.setAutoResize();
        FB.Canvas.setSize({ width: 810, height: 555 });
    }, 250);

  };

  // 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));
</script>
<script type="text/javascript">
function loging(getuid){
    jQuery("#loginfb").hide();
    jQuery("#fbload").show();
    FB.api(
              {
                method: 'fql.query',
                query: 'SELECT name, uid FROM user WHERE uid=' + getuid
              },
              function(response) {
                var user = response[0];
                jQuery.ajax({
                type: 'GET',
                url: 'login.php?uid=' + getuid,
                success: function(data) {
                    jQuery('#jstarget').html(data);
                    jQuery("#fbload").hide();
                }
                });
              }
            );
}


jQuery(document).ready(function() {
    // fetch the status on load
    jQuery('#loginfb').on('click', function() {

        if (FB.getAuthResponse()){
            FB.api('/me', function(response) {
            loging(response.id);
            });
        }
        else{
            //user is not connected to your app or logged out
            FB.login(function(response) {
                if(response.authResponse) {
                    FB.api('/me', function(response) {
                    loging(response.id);
                    });
                }
                else {
                    //user cancelled login or did not grant authorization
                }
            }, {scope:'publish_stream'});
        }
    });
    // handle a session response from any of the auth related calls
});
</script>

09-25 20:28