2012-04-05 87 views
3

我使用Facebook的JavaScript SDK來實現我的網站登錄後不設置訪問令牌的cookie中的鉻。我已經在Safari和Firefox上使用它,但在Chrome中卻沒有。Facebook上的JavaScript SDK的FB.login呼叫

中的FB.login方法被調用後,我能夠檢索從Facebook訪問令牌,但像它在Firefox和Safari瀏覽器也它不是在餅乾設置的訪問令牌和用戶ID。目前,我手動將訪問令牌和用戶ID寫入cookie以便登錄流程正常工作,但仍然讓我感到困惑,即訪問令牌不是單獨在Chrome中編寫的。

有沒有人碰到這個之前來了解在手的位置嗎?非常感激。

下面是我的init和登錄邏輯代碼:

window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : "#{AppConfig.facebook['app_id']}", // App ID 
    status  : true, // check login status 
    cookie  : true, // enable cookies to allow the server to access the session 
    xfbml  : true // parse XFBML 
    }); 

    // Additional initialization code here 
}; 

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

$(function() { 
    $(".fb-login-button").click(function() { 
    FB.login(function(response) { 
     if (response.authResponse) { 
     console.log("Welcome! Fetching your information..."); 
     $.cookie("user_id", response.authResponse.userID); 
     $.cookie("access_token", response.authResponse.accessToken); 
     window.location = "#{oauth_facebook_callback_url}"; 
     } else { 
     console.log("User cancelled login or did not fully authorize."); 
     } 
    }, {scope: 'publish_stream,offline_access,email,user_events,create_event,user_location'}); 
    }); 
}); 

回答

1

注意,JS SDK已經做存儲的觀衆最新的ID,並在fbsr_APPID餅乾ACCESS_TOKEN。每次用戶使用FB.uiFB.login,FB.getLoginStatusforce = trueFB.initstatus: true時,該cookie將使用最新數據進行更新。

我強烈建議使用這種方法,而不是存儲自己的餅乾,因爲它與你的應用程序的密鑰就像signed_request參數簽名。你應該按照那裏的指示來解碼它,驗證簽名,如果它是有效的,那就使用它。這將防止僞造(但仍可能無法防止短期重播攻擊)。