使用Django/Python连接Facebook

2 投票
1 回答
690 浏览
提问于 2025-04-16 03:24

我在这个问题上卡了很久。按照谷歌的教程,我就是无法让Facebook登录窗口弹出来。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
    FB.init({appId:'137101656332358', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.sessionChange', function(response) {
        if (response.session) {
            // A user has logged in, and a new cookie has been save
        } else {
            // The user has logged out, and the cookie has been cleared
        }
    });

在Firebug里我看到以下错误:

Firebug cannot find _firebugConsole element true Window localhost:8000
Firebug cannot find _firebugConsole element true Window localhost:8000
Firebug cannot find _firebugConsole element true Window localhost:8000
FB is not defined
[Break on this error] FB.provide('',{getLoginStatus:function...ern:FB._inCanvas?0:2});return a;}}}); 

我设置的 网站网址 = http://localhost:8000/

谢谢!

1 个回答

1

新的Facebook接口需要异步加载:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    //[here goes your code:]
    FB.init({appId:'137101656332358', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.sessionChange', function(response) {
        if (response.session) {
        // A user has logged in, and a new cookie has been save
        } else {
        // The user has logged out, and the cookie has been cleared
        }
    });
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
</script> 

撰写回答