每次页面加载时Facebook访问令牌都会发生变化

时间:2014-03-02 09:32:49

标签: javascript facebook api facebook-graph-api

Facebook API访问令牌在每个页面加载时是否都会更改?

我认为一旦获得一个代币,它将保持不变直到到期。

我正在使用Facebook SDK for Javascript。

Facebook表示,使用此SDK,无需手动管理访问令牌。 SDK就是这么做的。

但令牌在每个页面加载时是否更改是否正确?

我的代码是:

  <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>

2 个答案:

答案 0 :(得分:1)

我刚测试了这个,你是对的,Token随着每个页面刷新而改变。我不担心它,但是当使用其中一个SDK(JavaScript,PHP,...)时,你很可能根本不需要考虑访问令牌。 即使您需要它们(例如,用于管理Pages),您也可以使用最后一个。

“旧”代币仍然有效,但它们不会失效。但无论如何,他们将在2小时后停止工作。

还有第二个参数可以设置为“true”:https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/(参见“往返Facebook的服务器”) - 这可能已经解释了新的令牌,但你也没有使用它。 / p>

答案 1 :(得分:0)

Facebook最近改变了刷新访问令牌的过程。

如果在你的情况下适用,请尝试使用它:

https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN 

修改

这对你也有帮助。 Link