2011-08-22 325 views
0

這是我的代碼。它完美的Facebook的JavaScript SDK測試控制檯上,而不是在這裏:http://www.booktrolley.in/beta/fblogin.phpFacebook JavaScript SDK登錄

<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> 
+0

嗨再次:)我仍然認爲,從其他的一個它原本指向不同的域調用您的應用程序是問題。嘗試在您的應用程序域名「www.booktrolley.in」上創建「pedurology.org」上的相同頁面,並查看結果。 – ifaour

+0

@ifaour嗨:D我所有的數據都在pedurology.org上,我的我的應用程序域更改爲pedurology.org,正如您所建議的那樣,上一個問題已經解決。 但不是這一個。 感謝您的回覆:D – Anant

+0

或者您可以將您的網站,域名網址在您的應用設置中更改爲「pedurology.org」並嘗試使用。 – ifaour

回答

1

,我在下面的代碼得到一個「日誌是未定義」錯誤:

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頁面上的記錄器,但不是你自己的?

+0

對不起,我不太瞭解代碼。什麼是 - 記錄器在這裏? – Anant

+1

日誌是一些自定義記錄器Facebook內置到他們的測試頁面。將調試信息寫入瀏覽器的控制檯或頁面上的一些隱藏的div。無論哪種方式,記錄器都沒有在您的頁面上定義,所以您的腳本在它有機會做任何事情之前就會中斷。取出Log.info('onStatus',響應);線路,你應該很好去。 –

+0

它的工作! 感謝您的幫助。我真的很感激:D – Anant

1

試試這個代碼:

<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,因此您需要更改代碼以使用新的響應(請參閱此post)(您可能需要更改應用程序設置)
  3. 基於#2,當前用戶ID在使用showAccountInfo()將使用可訪問response.authResponse.userID