使用 Javascript SDK 刷新 FB 访问令牌
Refreshing FB Access Token with Javascript SDK
对于上下文,我正在尝试构建一个小型微型网站以从 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
});
};
FB.getLoginStatus 确实会刷新令牌,但不会在每次页面调用时刷新。它仅在令牌不再有效时刷新令牌。这就是过期数据不会改变的原因。
您可以将函数的第二个参数设置为 true
,以获得更准确的状态,但请注意,这可能会影响性能:
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true);
If you call FB.getLoginStatus on every page load, be careful not to
set this parameter for each as it will significantly increase the
number of requests to Facebook's servers, and thus decrease the
performance of your application.
旁注:您可以使用 FB.login
使登录变得不那么复杂(在我看来)- 这是一篇关于它的旧但仍然有效的文章:https://www.devils-heaven.com/facebook-javascript-sdk-login/
对于上下文,我正在尝试构建一个小型微型网站以从 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
});
};
FB.getLoginStatus 确实会刷新令牌,但不会在每次页面调用时刷新。它仅在令牌不再有效时刷新令牌。这就是过期数据不会改变的原因。
您可以将函数的第二个参数设置为 true
,以获得更准确的状态,但请注意,这可能会影响性能:
FB.getLoginStatus(function(response) {
// this will be called when the roundtrip to Facebook has completed
}, true);
If you call FB.getLoginStatus on every page load, be careful not to set this parameter for each as it will significantly increase the number of requests to Facebook's servers, and thus decrease the performance of your application.
旁注:您可以使用 FB.login
使登录变得不那么复杂(在我看来)- 这是一篇关于它的旧但仍然有效的文章:https://www.devils-heaven.com/facebook-javascript-sdk-login/