简体   繁体   English

使用 Javascript SDK 刷新 FB 访问令牌

[英]Refreshing FB Access Token with Javascript SDK

For context I am trying to build a small microsite to pull social media insights from Facebook. I am looking to build a Django backend and React front end.对于上下文,我正在尝试构建一个小型微型网站以从 Facebook 获取社交媒体见解。我希望构建一个 Django 后端和 React 前端。

I would like to implement FB login with the Javascript SDK since according to the docs this refreshes the access token each time a FB call is made using the SDK.我想使用 Javascript SDK 实现 FB 登录,因为根据文档,每次使用 SDK 进行 FB 调用时,这都会刷新访问令牌。

I have a very simple login script taken from FB official docs which literally just logs in the user and then outputs the list of accounts the user has access to.我有一个取自 FB 官方文档的非常简单的登录脚本,它实际上只是登录用户,然后输出用户有权访问的帐户列表。

The issue is that, despite refreshing the page (and therefore performing an API request) the data expiration date doesn't extend (it still displays the date of first login).问题是,尽管刷新了页面(并因此执行了 API 请求),数据到期日期并没有延长(它仍然显示首次登录的日期)。

Is anyone familiar with the Javascript SDK and whether this is normal behaviour?有人熟悉 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 does refresh the Token, but not on every Page call. FB.getLoginStatus确实会刷新令牌,但不会在每次页面调用时刷新。 It only refreshes the Token when it is not valid anymore.它仅在令牌不再有效时刷新令牌。 That is why the expiration data does not change.这就是到期数据不会更改的原因。

You can can a more accurate status with the second parameter of the function set to true , but be aware that this might affect performance:您可以将 function 的第二个参数设置为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.getLoginStatus,请注意不要为每个页面设置此参数,因为它会显着增加对 Facebook 服务器的请求数量,从而降低应用程序的性能。

Side note: ou can use FB.login to make the login less complicated (in my opinion) - here's an old but still valid article about that: https://www.devils-heaven.com/facebook-javascript-sdk-login/旁注:您可以使用FB.login使登录变得不那么复杂(在我看来)——这是一篇关于它的旧但仍然有效的文章: https://www.devils-heaven.com/facebook-javascript-sdk-login /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM