Facebook PHP SDK长期访问令牌在刷新站点后过期 [英] Facebook PHP SDK Long-lived access token expires after refresh site

查看:269
本文介绍了Facebook PHP SDK长期访问令牌在刷新站点后过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Facebook SDK v5作为PHP,并且我试图获得长久的USER访问令牌。

我得到它,长久一次保存到我的数据库,然后我去另一个页面,我用它来访问Facebook PAGE。在这一点上,一切顺利。然后刷新我的页面,用户AccessToken的[到期设置为1970-01-01。

我不知道发生了什么,因为我不会在数据库中重写这个访问令牌。最有意思的是,这个标记与Facebook一起使用。所以在刷新之前和之后有我的代码和响应。

I'm using Facebook SDK v5 for PHP and I'm trying to get long-lived USER access token.
I'm getting it, makes long-lived once and save to my database, then I go to another page where I use it to get accesstoken to Facebook PAGE. To this point everything goes fine. Then I refresh my page, and User AccessToken has ["expiry at"] set to 1970-01-01.
I have no idea what is happening, because I don't rewrite this access token in database. And the most wired thing is that, this token works with facebook. So there is my code and response before and after refreshing.

$config = array();
        $config['app_id'] = xxx
        $config['app_secret'] = xxx
        $config['fileUpload'] = false;        
        if(!empty(tokenFromDatabse)){            
            $config['default_access_token'] = tokenFromDatabse;
        }        
        $fb = new Facebook($config);
        $oAuth2Client = $fb->getOAuth2Client();
        if(!empty(tokenFromDatabse)){
            try{    
                $tokenMetadata = $oAuth2Client->debugToken($fb->getDefaultAccessToken());
                $tokenMetadata->validateAppId($config['app_id']);
                $tokenMetadata->validateExpiration();
                }

这只是所有这些动作所做的,当然有代码。现在,Facebook的第一个回应是:

It's just all what that action do, there is of course catch code. And now, first response from facebook is:

object(Facebook\Authentication\AccessTokenMetadata)#689 (1) {
  ["metadata":protected]=>
  array(7) {
    ["app_id"]=>
    string(15) "xxx"
    ["application"]=>
    string(13) "Local_app"
    ["expires_at"]=>
    object(DateTime)#691 (3) {
      ["date"]=>
      string(26) "2015-10-20 16:07:56.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    ["is_valid"]=>
    bool(true)
    ["issued_at"]=>
    object(DateTime)#692 (3) {
      ["date"]=>
      string(26) "2015-08-21 16:07:56.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(13) "Europe/Berlin"
    }
    ["scopes"]=>
    array(3) {
      [0]=>
      string(12) "manage_pages"
      [1]=>
      string(13) "publish_pages"
      [2]=>
      string(14) "public_profile"
    }
    ["user_id"]=>
    string(15) "xxx"
  }

刷新后看起来像那个:

And after refresh looks like that:

object(Facebook\Authentication\AccessTokenMetadata)#689 (1) {
      ["metadata":protected]=>
      array(7) {
        ["app_id"]=>
        string(15) "xxx"
        ["application"]=>
        string(13) "Local_app"
        ["expires_at"]=>
        object(DateTime)#691 (3) {
          ["date"]=>
          string(26) "1970-01-01 01:00:00.000000"
          ["timezone_type"]=>
          int(3)
          ["timezone"]=>
          string(13) "Europe/Berlin"
        }
        ["is_valid"]=>
        bool(true)
        ["issued_at"]=>
        object(DateTime)#692 (3) {
          ["date"]=>
          string(26) "2015-08-21 16:07:56.000000"
          ["timezone_type"]=>
          int(3)
          ["timezone"]=>
          string(13) "Europe/Berlin"
        }
        ["scopes"]=>
        array(3) {
          [0]=>
          string(12) "manage_pages"
          [1]=>
          string(13) "publish_pages"
          [2]=>
          string(14) "public_profile"
        }
        ["user_id"]=>
        string(15) "xxx"
      }


推荐答案

扩展页面访问令牌根本没有任何默认到期。并且由于实现细节,用于请求扩展页面访问令牌的用户访问令牌也会无限期有效。

Extended page access tokens do not have any default expiry at all. And due to implementation details, a user access token used to request an extended page access token becomes valid "indefinitely" as well.

因此,到期值将为0,null或类似的东西。当格式化为日期时,这将变为1970-01-01,因为那是unix纪元的开始。

So the expiry value will be 0, null or something like that. And when formatted as a date, that becomes 1970-01-01, since that is the begin of the unix epoch.

这篇关于Facebook PHP SDK长期访问令牌在刷新站点后过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆