對于背景關系,我正在嘗試建立一個小型微型網站,以從 Facebook 中獲取社交媒體見解。我正在尋找構建 Django 后端和 React 前端。
我想使用 Javascript SDK 實作 FB 登錄,因為根據檔案,每次使用 SDK 進行 FB 呼叫時都會重繪 訪問令牌。
我有一個非常簡單的登錄腳本,取自 FB 官方檔案,它實際上只是登錄用戶,然后輸出用戶有權訪問的帳戶串列。
問題是,盡管重繪 了頁面(并因此執行了 API 請求),資料到期日期并沒有延長(它仍然顯示第一次登錄的日期)。
有沒有人熟悉 Javascript SDK 以及這是否是正常行為?
function statusChangeCallback(response) { // Called with the results from FB.getLoginStatus().
console.log(response);
if (response.status === 'connected') { // Logged into your webpage and Facebook.
testAPI();
FB.getAuthResponse(function(response){
console.log(response.authResponse.accessToken);
});
} else { // Not logged into your webpage or we are unable to tell.
document.getElementById('status').innerHTML = 'Please log '
'into this webpage.';
}
}
function checkLoginState() { // Called when a person is finished with the Login Button.
FB.getLoginStatus(function(response) { // See the onlogin handler
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : 'APP_ID_GOES_HERE',
cookie : true, // Enable cookies to allow the server to access the session.
xfbml : true, // Parse social plugins on this webpage.
version : 'v13.0' // Use this Graph API version for this call.
});
FB.getLoginStatus(function(response) { // Called after the JS SDK has been initialized.
statusChangeCallback(response); // Returns the login status.
});
};
function testAPI() { // Testing Graph API after login. See statusChangeCallback() for when this call is made.
console.log('Welcome! Fetching your information.... ');
FB.api('/me/accounts',
function(response) {
console.log(response)
});
}
function logout_js_user(){
FB.logout(function(response) {
// Person is now logged out
});
};
uj5u.com熱心網友回復:
FB.getLoginStatus確實會重繪 令牌,但不是在每次 Page 呼叫時重繪 。它僅在令牌不再有效時重繪 令牌。這就是到期資料不改變的原因。
您可以將函式的第二個引數設定為更準確的狀態true,但請注意這可能會影響性能:
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true);
如果您在每次頁面加載時呼叫 FB.getLoginStatus,請注意不要為每個頁面設定此引數,因為它會顯著增加對 Facebook 服務器的請求數量,從而降低應用程式的性能。
旁注:您可以使用FB.login使登錄不那么復雜(在我看來) - 這是一篇舊但仍然有效的文章:https ://www.devils-heaven.com/facebook-javascript-sdk-login/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/475491.html
標籤:javascript Facebook sdk facebook-javascript-sdk
上一篇:(BreakingAgain)SDK尚未初始化,請務必先呼叫FacebookSdk.sdkInitialize()。(安卓)
