[英]Facebook JavaScript SDK does not set access token cookie in chrome after FB.login call
我正在使用 Facebook JavaScript SDK 为我的网站实现登录。 我已经让它在 Safari 和 Firefox 上运行,但在 Chrome 中不行。
调用 FB.login 方法后,我能够从 Facebook 检索访问令牌,但它没有像在 Firefox 和 Safari 中那样在 cookies 中设置访问令牌和用户 ID。目前,我正在手动编写访问令牌和用户 ID 到 cookies 所以登录流程有效,但它仍然让我感到烦恼,因为访问令牌不是单独在 Chrome 中编写的。
有没有人以前遇到过这个并且知道手头的修复方法? 非常感激。
以下是我的初始化和登录逻辑代码:
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'});
});
});
如果您在本地测试您的应用程序,请注意 Google Chrome 浏览器不支持本地文件(包括localhost
)的 cookies ,除非您使用--enable-file-cookies
标志启动它。 但是,如果您使用本地 IP 地址( 127.0.0.1
)进行测试,Chrome 确实支持 cookies。
您可以在此处查看有关此行为的讨论: http://code.google.com/p/chromium/issues/detail?id=535
请注意,JS SDK 已经将查看者的最新 ID 和 access_token 存储在fbsr_APPID cookie 中。 每当用户使用FB.ui
、 FB.login
、 FB.getLoginStatus
with force = true
或FB.init
with status: true
时,该 cookie 将更新为最新数据。
我强烈建议使用此方法而不是存储您自己的 cookies,因为它与signed_request参数一样使用您应用程序的密钥进行签名。 您应该按照那里的说明对其进行解码,验证签名,如果有效,请使用它。 这将防止伪造(但仍然可能无法防止短期重放攻击)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.