时间:2010-07-25 21:25:09

标签: facebook facebook-javascript-sdk

6 个答案:

答案 0 :(得分:2)

我不知道你的代码是什么,但我的问题是我忘了添加 <div id="fb-root"></div>。我的代码:

<div id="fb-root"></div>
<script src="http://static.ak.fbcdn.net/connect/en_US/core.js"></script>
<script>
    FB.init({ apiKey: 'app key'});
</script>
<div class="fbloginbutton" id="fb-login" onclick="Login();">
<span id="fb_login_text" >Login with Facebook</span>
</div>
<asp:Label ID="errMsg" runat="server"></asp:Label>
<script type="text/javascript">
    function Login() {
        FB.login(function(response) {
            document.getElementById('fb_login_text').innerHTML = 'Logout';
            if (response.session) {
                FB.api('/me', function(response) {
                    var str;
                    str = response['id'] + ";" +
                            response['name'] + ";" +
                            response['first_name'] + ";" +
                            response['last_name'] + ";" +
                            response['link'] + ";" +
                            response['birthday'] + ";" +
                            response['gender'] + ";" +
                            response['email'];
                    alert(str);
                });
            }
            else {
                document.getElementById('fb_login_text').innerHTML = 'Login with Facebook';
            }
        }, { perms: 'user_birthday,email' });
    };
</script> 

如你所见,我不在任何地方使用div fb-root,但它被重新加入facebook登录工作!

答案 1 :(得分:2)

当facebook最近对其“all.js”进行更改时,我的网站出现了这个问题。

在他们的javascript的早期版本中,我遇到了一个特定于IE的问题,我从互联网上某人的帖子中复制了这一小段代码。它看起来很神秘,但当时解决了这个问题:

  // http://developers.facebook.com/bugs/204792232920393

  // Hack to fix http://bugs.developers.facebook.net/show_bug.cgi?id=20168 for IE7/8/9.
  FB.UIServer.setLoadedNode = function (a, b) { FB.UIServer._loadedNodes[a.id] = b; };
  FB.UIServer.setActiveNode = function(a, b) { FB.UIServer._active[a.id]=b; };

事实证明这些线条对我造成了这个问题。我删除了它们,问题就消失了。我认为,特定于IE的原始错误也已在最近的“all.js”中得到修复。

答案 2 :(得分:2)

我最近在这个问题上挣扎。这个问题从没有出现过,大概来自Facebook JS SDK的一些变化。为了我的计划永远不再使用JS SDK,这些随机问题会耗费我的时间。

无论如何,这是我过去常常解决的问题。

var accessToken, fb_response;

if (window.location.hash.length < 30) {
  window.location = "http://www.facebook.com/dialog/oauth?client_id=YOUR_ID&redirect_uri=YOUR_URL&scope=YOUR_PERMISSIONS&response_type=token";
} else {
  fb_response = window.location.hash;
  accessToken = fb_response.substr(14, fb_response.indexOf("&expires") - 14);
  FB._authResponse = {
    accessToken: accessToken
  };
  window.location.hash = "";
  FB.api("/me", function(profile) {
    if (profile.id) {
       app_init();
    } else {
       alert("Problem connecting to Facebook");
    }
  }); 
}

基本上我将用户发送到Facebook oauth对话框,当他们返回时,我从哈希中获取访问令牌。然后,我在Facebook对象上设置内部访问令牌参数。完成此操作后,您可以进行所有正常的Facebook Api呼叫。

希望这有助于某人! 对于未来的项目,我一定会坚持服务器端auth流程,你有更多的控制权!

答案 3 :(得分:1)

您似乎正在尝试使用本地主机,可以使用公共网址进行尝试。

我已经遇到过这个问题。但是我通过在应用程序中将canvas url配置为公共URL来解决它(例如.www.something.com / test /)。

答案 4 :(得分:1)

这是我的工作样本:

<!DOCTYPE html>
<html>
  <head>
    <title>Facebook Client-side Authentication Example</title>
  </head>
  <body>
    <div id="fb-root"></div>
    <script>
      // 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));

      // Init the SDK upon load
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '00000000000000', // Your App ID
          channelUrl : '//'+window.location.hostname+'/channel', // Path to your Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });

        // listen for and handle auth.statusChange events
        FB.Event.subscribe('auth.statusChange', function(response) {
          if (response.authResponse) {
            // user has auth'd your app and is logged into Facebook
            FB.api('/me', function(me){
              if (me.name) {
                document.getElementById('auth-displayname').innerHTML = me.name;
              }
            })
            document.getElementById('auth-loggedout').style.display = 'none';
            document.getElementById('auth-loggedin').style.display = 'block';
          } else {
            // user has not auth'd your app, or is not logged into Facebook
            document.getElementById('auth-loggedout').style.display = 'block';
            document.getElementById('auth-loggedin').style.display = 'none';
          }
        });

        // respond to clicks on the login and logout links
        document.getElementById('auth-loginlink').addEventListener('click', function(){
          FB.login();
        });
        document.getElementById('auth-logoutlink').addEventListener('click', function(){
          FB.logout();
        }); 
      } 
    </script>

    <h1>Facebook Client-side Authentication Example</h1>
      <div id="auth-status">
        <div id="auth-loggedout">
          <a href="#" id="auth-loginlink">Login</a>
        </div>
        <div id="auth-loggedin" style="display:none">
          Hi, <span id="auth-displayname"></span>  
        (<a href="#" id="auth-logoutlink">logout</a>)
      </div>
    </div>

  </body>
</html>

答案 5 :(得分:0)

该网站明确表示all.js已过期。但是,我得到了与sdk.js相同的错误。回到折旧的all.js

时问题得到解决
// Old SDK (deprecated)
js.src = "//connect.facebook.net/en_US/all.js";

// New SDK (v2.x)
js.src = "//connect.facebook.net/en_US/sdk.js";

facebook sdk