0

我正在尝试将 Facebook 登录名与我的网站集成,并且我现在正在通过本地主机测试我的 Facebook 登录名。这是我按照 Facebook JavaScript SDK 指南上的说明生成的代码。

<html>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '*************', // App ID
      channelUrl : 'http://localhost', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // connected
    testAPI();
  } else if (response.status === 'not_authorized') {
    // not_authorized
    login();
  } else {
    // not_logged_in
    login();
  }
 });
  };

  function login() {
    FB.login(function(response) {
        if (response.authResponse) {
            // connected
            testApi();
        } else {
            // cancelled
            console.log('Login failed!');
        }
    });

    function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
        console.log('Good to see you, ' + response.name + '.');
    });
}
}

  (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 src="//connect.facebook.net/en_US/all.js"></script>
</body>
</html>

就是说加载资源失败:file://connect.facebook.net/en_US/all.js基本上是 GET all.js 失败。怎么了?

我的 Facebook 应用程序仪表板localhost的 AppDomain 和站点 URL 为http://localhost.

4

2 回答 2

0

试试下面的代码。

(function(){
var e   = document.createElement('script');
e.type  = 'text/javascript';
e.src   = 'http://connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
于 2013-02-22T07:33:15.727 回答
0

2018 年更新的答案:

1)测试HTML文件无法加载,因为file:// 它需要在Web服务器下加载(例如http://localhost/test.html:)

2) 禁用脚本/广告拦截器。他们中的大多数人将 FB SDK 视为“邪恶”并阻止它。

于 2018-01-14T16:54:16.650 回答