Extending Access Token Expiration not functioning

2020-04-13 17:37发布

I am at the intermediate level in php and am new with facebook development. I have looked through the facebook documents and Stack Overflow previous comments.

All I basically wanted to do was let the user log in with their Facebook account and display their name.

My php page has a graph, and the page auto refreshes every 2 or 5 min.

I authenticate and get the facebook first_name to put on the page.

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

echo $graph['first_name'] to get the first name of the user .. (for which I thought that no access token was required).

After about 90 min. I have been receiving the error:

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

and I have no value ( 0 ), in the $facebook->getUser(); parameter

I do know that off line access permission has been depreciated, (and I have have this enabled in my apps advanced settings)

I am trying to get an extended access token. In the FB docs. I see:

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

I included my information in the link(an existing valid access token and all) and received a access token:

access_token=AAADbZBPuUyWwBAFubPaK9E6CnNsPfNYBjQ9OZC63ZBN2Ml9TCu9BYz89frzUF2EnLttuZAcG2fWZAHbWozrvop9bQjQclxVYle7igvoZCYUAg2KNQLMgNP&expires=4050

Yet this token expired in about 1 hour or so.(....expires=4050)

I assume I am using server side auth because I am using PHP?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-13 18:02

I assume you need to enable "deprecate offline_access" in your Apps Advanced Settings page. As this worked for me:


//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'];
}

The token can still be invalid for several reasons, See How-To: Handle expired access tokens. Hope it helps

查看更多
Juvenile、少年°
3楼-- · 2020-04-13 18:16

There's a bug on this: https://developers.facebook.com/bugs/241373692605971

But, another question on SO has a workaround (user uninstalls and re-installs): fb_exchange_token for PHP only working once user removes app

查看更多
登录 后发表回答