7

我正在尝试在网站上实施 Facebook Connect。我正在尝试使用 Facebok 的 Javascript SDK。我是新手,不幸的是,Facebook WIKI 中提供的大多数链接都已过时...找不到返回 404。无论如何,我在结尾之前添加了这段代码</body>

<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
 <div id="fb-root"></div>
 <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"> </script>
 <script src="http://connect.facebook.net/en_US/all.js"></script>
 <script>
   FB.init({
     appId  : '12344', // my real app id is here
     status : true, // check login status
     cookie : true, // enable cookies to allow the server to access the session
     xfbml  : false  // parse XFBML
   });

   FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
        alert('user is logged in and granted some permissions');
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
        alert('user is logged in, but did not grant any permissions');
      // user is logged in, but did not grant any permissions
    }
  } else {
        alert('user is not logged in');
    // user is not logged in
  }
}, {perms:'email'});
 </script>

我在其他地方(在上述脚本之前)有一个登录按钮,在同一个页面中呈现:

<fb:login-button v="2">Connect with Facebook</fb:login-button>

此按钮呈现为普通的 fb 连接按钮,单击它会像通常那样打开一个新的弹出窗口。问题是它显示“指定的 api 密钥无效”错误。在检查弹出窗口的地址栏时,我看到 api_key=undefined 被传递给它:(

对此有任何想法吗?我现在正在尝试修复此问题 5 小时...请帮助我找出为什么没有将正确的 API 密钥传递给弹出窗口。

提前致谢。

4

3 回答 3

4

我也在做同样的事情,我找到了一个非常易于实现和理解的示例(这里使用 jQuery,但你可以在没有库的情况下做同样的事情):

<body>
  <div>
    <button id="login">Login</button>
    <button id="disconnect">Disconnect</button>
  </div>
  <div id="user-info" style="display: none;"></div>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js"></script>
  <script>
    // initialize the library with the API key
    FB.init({ appId  : '12344' });

    // fetch the status on load
    FB.getLoginStatus(handleSessionResponse);

    $('#login').bind('click', function() {
      FB.login(handleSessionResponse);
    });

    $('#disconnect').bind('click', function() {
      FB.api({ method: 'Auth.revokeAuthorization' }, function(response) {
        clearDisplay();
      });
    });

    // no user, clear display
    function clearDisplay() {
      $('#user-info').hide('fast');
    }

    // handle a session response from any of the auth related calls
    function handleSessionResponse(response) {
      // if we dont have a session, just hide the user info
      if (!response.session) {
        clearDisplay();
        return;
      }

      // if we have a session, query for the user's profile picture and name
      FB.api(
        {
          method: 'fql.query',
          query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
        },
        function(response) {
          var user = response[0];
          $('#user-info').html('<img src="' + user.pic + '">' + user.name).show('fast');
        }
      );
    }
  </script>
</body>

在此处查找完整代码,在这里您可以找到其他资源。

于 2010-04-27T18:14:53.650 回答
3

我已将我的 facebook 连接地址设置为 www,并且当我切换到 www 版本 fb 连接工作正常时,我正在使用非 www 访问我的测试站点。

于 2010-07-25T16:46:24.570 回答
1

您需要设置画布授权后 url,并确保基本画布 url 是授权后 url 的前缀。

于 2010-06-24T00:18:31.150 回答