Facebook API 访问 token 在每次页面加载时都会发生变化,这是否正确?
我认为一旦获得 token ,它就会保持不变直到到期。
我正在使用 Facebook SDK for Javascript。
Facebook 表示,使用此 SDK,无需手动管理访问 token 。 SDK 就可以做到这一点。
但是 token 在每次页面加载时都会发生变化,这是正确的吗?
我的代码是这样的:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxx',
status : true,
xfbml : false,
cookie : true
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function check() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
console.log(response);
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
console.log(response);
} else {
// the user isn't logged in to Facebook.
console.log(response);
}
});
}
</script>
<span onClick="check()">test</span>
最佳答案
我刚刚对此进行了测试,您是对的, token 会随着每次页面刷新而变化。不过,我不会太担心,当使用其中一种 SDK(JavaScript、PHP 等)时,您很可能根本不需要考虑访问 token 。 即使您需要它们(例如,用于管理页面),您也可以只使用最后一个。
顺便说一句,“旧” token 仍然有效,它们不会失效。但无论如何他们都会在 2 小时后停止工作。
还有第二个参数可以设置为“true”:https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/ (请参阅“Facebook 服务器的往返”)- 这可能解释了新 token ,但您也没有使用它。
关于javascript - Facebook 访问 token 在每次页面加载时都会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125951/