1

对于我创建的许多 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
}

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

4

1 回答 1

0

您需要删除所有多个 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>
于 2012-04-29T16:36:59.077 回答