Facebook JavaScript SDK Login

Facebook JavaScript SDK Login

本文关键字:Login SDK JavaScript Facebook      更新时间:2023-09-26

这是我的代码。它可以完美地运行在Facebook的JavaScript SDK测试控制台,但不是在这里:http://www.booktrolley.in/beta/fblogin.php

<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
<head></head> 
<body> 
<div id="fb-root"></div> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
FB.init({
  appId  : '270423476307006',
 status : true, // check login status
 cookie : true, // enable cookies to allow the server to access the session
 xfbml  : true, // parse XFBML
channelUrl : 'http://pedurology.org/booktrolley/beta/channel.html', // channel.html file
oauth  : true // enable OAuth 2.0
});
</script> 
<h1>Login, Logout, Name and Profile Picture</h1> 
<div id="account-info"></div> 
<script> 
/**
 * This assumes the user is logged in and renders their profile picture,
 * name and a logout link.
*/
  function showAccountInfo() {
  FB.api(
 {
   method: 'fql.query',
  query: 'SELECT name,uid,pic_square FROM user WHERE uid='+FB.getSession().uid
 },
 function(response) {
 alert(FB.getSession().uid);
  Log.info('API Callback', response);
  document.getElementById('account-info').innerHTML = (
    '<img src="' + response[0].pic_square + '"> ' +
    response[0].name +
    ' <img onclick="FB.logout()" style="cursor: pointer;"' +
        'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
   );
  }
  );
}
/**
 * This assumes the user is logged out, and renders a login button.
 */
 function showLoginButton() {
   document.getElementById('account-info').innerHTML = (
 '<img onclick="FB.login()" style="cursor: pointer;"' +
     'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
  );
}
/**
* This will be called once on page load, and every time the status changes.
*/
function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
 showAccountInfo();
  } else {
  showLoginButton();
 }
 }
 FB.getLoginStatus(function(response) {
  onStatus(response); // once on page load
 FB.Event.subscribe('auth.statusChange', onStatus); // every status change
 });
 </script> 
  </body> 
 </html> 

我在下面的代码中得到一个"Log is undefined"错误:

function onStatus(response) {
Log.info('onStatus', response);
if (response.session) {
 showAccountInfo();
  } else {
  showLoginButton();
 }
 }
 FB.getLoginStatus(function(response) {
  onStatus(response); // once on page load
 FB.Event.subscribe('auth.statusChange', onStatus); // every status change
 });

你试图利用一个存在于FB的页面,但不是你自己的记录器?

试试下面的代码:

<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head></head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: '270423476307006',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });
    FB.getLoginStatus(function(response) {
        onStatus(response); // once on page load
        FB.Event.subscribe('auth.statusChange', onStatus); // every status change
    });
};
(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>
<h1>Login, Logout, Name and Profile Picture</h1>
<div id="account-info"></div>
<script>
/**
 * This assumes the user is logged in and renders their profile picture,
 * name and a logout link.
 */
function showAccountInfo(resp) {
    if( !resp.userID ) {
        // we shouldn't be here
        return;
    }
    FB.api(
    {
      method: 'fql.query',
      query: 'SELECT name,uid,pic_square FROM user WHERE uid='+resp.userID
    },
    function(response) {
      document.getElementById('account-info').innerHTML = (
        '<img src="' + response[0].pic_square + '"> ' +
        response[0].name +
        ' <img onclick="FB.logout()" style="cursor: pointer;"' +
            'src="https://s-static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">'
      );
    }
    );
}
/**
 * This assumes the user is logged out, and renders a login button.
 */
function showLoginButton() {
  document.getElementById('account-info').innerHTML = (
    '<img onclick="FB.login()" style="cursor: pointer;"' +
         'src="https://s-static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">'
  );
}
/**
 * This will be called once on page load, and every time the status changes.
 */
function onStatus(response) {
  if (response.authResponse) {
    showAccountInfo(response.authResponse);
  } else {
    showLoginButton();
  }
}
</script>
</body>

现在让我们解释一下我们做了什么:

  1. 我们使用异步加载来确保FB.getLoginStatus()只在库成功加载时才会被触发
  2. 既然你把oauth设置为true,那么你需要改变你的代码来处理新的响应(参考这篇文章)(你可能需要改变你的应用程序设置)
  3. 基于#2,在showAccountInfo()中使用的当前用户ID将可以使用response.authResponse.userID
  4. 访问