Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Access Token Expiration not functioning

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?

like image 340
Cee Bee Avatar asked Jan 31 '26 22:01

Cee Bee


1 Answers

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

like image 76
Sudhir Bastakoti Avatar answered Feb 03 '26 11:02

Sudhir Bastakoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!