Django - 如何在投入生产之前设置和测试Facebook登录?

时间:2015-10-24 22:56:18

标签: django facebook

所以我正在尝试将Facebook登录添加到我的页面,但我遇到了一些困难。我想在开始制作之前弄清楚如何在开发中测试这些功能,但我不完全理解如何做到这一点。

我已经按照facebook api的说明进行操作,所以我认为我有一些应该可行但我无法测试的东西。该代码如下:

<script>
  function fb_login() {
    FB.login( function() {}, { scope: 'email,public_profile' } );
  }

  // This is called with the results from from FB.getLoginStatus().
  function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }

  // This function is called when someone finishes with the Login
  // Button.  See the onlogin handler attached to it in the sample
  // code below.
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }

  window.fbAsyncInit = function() {
  FB.init({
    appId      : '************',
    cookie     : true,  // enable cookies to allow the server to access
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.2' // use version 2.2
  });

  // Now that we've initialized the JavaScript SDK, we call
  // FB.getLoginStatus().  This function gets the state of the
  // person visiting this page and can return one of three states to
  // the callback you provide.  They can be:
  //
  // 1. Logged into your app ('connected')
  // 2. Logged into Facebook, but not your app ('not_authorized')
  // 3. Not logged into Facebook and can't tell if they are logged into
  //    your app or not.
  //
  // These three cases are handled in the callback function.

  FB.getLoginStatus(function(response) {
    statusChangeCallback(response);
  });

  };

  // Load the SDK asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

  // Here we run a very simple test of the Graph API after login is
  // successful.  See statusChangeCallback() for when this call is made.
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
  </script>

我的应用程序ID在我的应用程序中是正确的。

这是我的登录按钮:

<a href="#" onclick="fb_login();"><img src="../../static/textchange/facebookloginbutton.png" border="0" alt="" height="42px;"></a>
        </div>

我自己做了所以我可以选择另一张图片。

所以现在当我尝试登录时出现此错误:

“应用程序配置不允许使用URL:应用程序的设置不允许使用一个或多个给定的URL。它必须与网站URL或Canvas URL匹配,或者域必须是其中一个的子域App的域名。“

所以我相信我必须选择某种类型的其他域来测试它?但我不明白我应该选择什么?我必须开始“托管”,以便我可以使用真正的域名吗?我可以在当地做吗?

我很感激能帮助我朝着正确的方向前进。

感谢。

2 个答案:

答案 0 :(得分:0)

您可以在Facebook开发者信息中心的客户端OAuth设置下配置有效的OAuth重定向URI,以允许您在本地使用的任何网址。或者将网站网址设置为使用您的本地网址。

答案 1 :(得分:0)

您的网站网址应为push_back。我认为http://localhost:8000/很重要

http://