如何使用Facebook社交登录登录我的网站

时间:2017-05-03 14:53:38

标签: facebook facebook-graph-api login facebook-javascript-sdk

我使用facebook登录到我的网站但我一直得到这个问题,当我点击Facebook登录按钮弹出显示并立即隐藏

我已经尝试使用facebook文档代码进行网络登录

<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
  // 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 {
      // The person is not logged into your app or we are unable to tell.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    }
  }

  // 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      : '{your-app-id}',
    cookie     : true,  // enable cookies to allow the server to access 
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.8' // use graph api version 2.8
  });

  // 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>

<!--
  Below we include the Login Button social plugin. This button uses
  the JavaScript SDK to present a graphical Login button that triggers
  the FB.login() function when clicked.
-->

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>

<div id="status">
</div>

</body>
</html>
  

我使用下面的链接

https://developers.facebook.com/docs/facebook-login/web

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

Facebook社交插件:登录按钮Facebook提供了许多他们称之为社交插件的东西。我们感兴趣的是登录按钮。以下是将按钮导入网页的基本步骤:

您的网站需要一个Facebook应用。转到https://developers.facebook.com/apps/,然后点击&#34;新应用&#34;按钮。

您的应用程序显示名称&#34;可以是有意义的事情。我通常会使用该网站的域名。您也可以将此同一个应用程序用于其他目的。 &#34;应用程序命名空间&#34;需要是不包含任何特殊字符的东西。我通常使用我的应用程序显示名称,没有空格,句点等。现在你需要输入你的应用程序域&#34;和网站&#34;网站网址。&#34;对于应用程序域,请输入托管您站点的域名。对于网站网址,请输入您网站的完整网址。例如,如果您的域名是name.com,那么您的网站网址应该是http://www.your_domain.com。输入正确的信息后,保存更改。现在,您将拥有一个显示您的页面的应用程序ID&#34;和#34; App Secret&#34;。只需将该页面保持打开状态,因为在将登录按钮代码添加到页面时,您将需要自己的应用程序ID。现在我们需要从Facebook获取登录按钮代码。转到http://developers.facebook.com/docs/reference/plugins/login/,然后点击&#34;获取代码&#34;按钮。从第一个框中复制HTML5代码,现在将其放在网页正文标签下方。代码看起来像这样:

<div id="fb-root"></div>
<script>
   (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/all.js#xfbml=1&appId=APP_ID";
     fjs.parentNode.insertBefore(js, fjs);
   } (document, 'script', 'facebook-jssdk'));
</script>

在代码中,请务必将APP_ID替换为您为Facebook应用创建的应用ID。现在,将第二个框中的代码粘贴到您希望登录按钮驻留的任何位置。代码应该类似于:

<div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1"></div>

data-show-faces等于false,因为当它设置为true时,注销按钮将在用户登录时显示。