扩展访问令牌到期不起作用

时间:2012-02-02 05:08:15

标签: facebook-php-sdk token

我处于php的中级水平,并且是facebook开发的新手。我查看了facebook文档和Stack Overflow以前的评论。

我基本上想做的就是让用户使用他们的Facebook帐户登录并显示他们的名字。

我的php页面有一个图表,页面每2或5分钟自动刷新一次。

我进行身份验证并将facebook first_name放到页面上。

 $graph = $facebook->api('/me');

echo $ graph ['first_name']获取用户的第一个名字..(我认为不需要访问令牌)。

大约90分钟后。我一直收到错误:

fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user......

我在$ facebook-> getUser()中没有值(0);参数

我知道离线访问权限已被弃用,(我已在我的应用高级设置中启用此功能)

我正在尝试获取扩展访问令牌。在FB文档中。我明白了:

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

我在链接中包含了我的信息(现有的有效访问令牌和所有),并收到了访问令牌:

access_token=AAADbZBPuUyWwBAFubPaK9E6CnNsPfNYBjQ9OZC63ZBN2Ml9TCu9BYz89frzUF2EnLttuZAcG2fWZAHbWozrvop9bQjQclxVYle7igvoZCYUAg2KNQLMgNP&expires=4050

然而,这个令牌在约1小时左右到期。(.... expires = 4050)

我假设我使用的是服务器端身份验证,因为我使用的是PHP?

2 个答案:

答案 0 :(得分:7)

我假设您需要在“应用高级设置”页面中启用“弃用offline_access”。因为这对我有用:


//added code in base_facebook.php inside the facebook class
public function getExtendedAccessToken(){

    try {
        // need to circumvent json_decode by calling _oauthRequest
          // directly, since response isn't JSON format.
        $access_token_response =
            $this->_oauthRequest(
                $this->getUrl('graph', '/oauth/access_token'),
                $params = array(    'client_id' => $this->getAppId(),
                                    'client_secret' => $this->getAppSecret(),
                                    'grant_type'=>'fb_exchange_token',
                                    'fb_exchange_token'=>$this->getAccessToken(),
                              ));

    } catch (FacebookApiException $e) {
      // most likely that user very recently revoked authorization.
      // In any event, we don't have an access token, so say so.
      return false;
    }

    if (empty($access_token_response)) {
      return false;
    }

    $response_params = array();
    parse_str($access_token_response, $response_params);
    if (!isset($response_params['access_token'])) {
      return false;
    }

    return $response_params['access_token'];
}

由于多种原因,令牌仍然无效,请参阅How-To: Handle expired access tokens。 希望它有所帮助

答案 1 :(得分:0)

这有一个错误: https://developers.facebook.com/bugs/241373692605971

但是,关于SO的另一个问题有一个解决方法(用户卸载并重新安装): fb_exchange_token for PHP only working once user removes app

相关问题