0

I try to get 60 days facebook access token from server side request. After user is loged in I check access token through getAccessToken() function from Facebook SDK and after that I use getExtendedAccessToken() which I found somewhere here: How to extend access token validity since offline_access deprecation to ask for 60 days access token:

   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->getApiSecret(),
                                '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'];
        }

Unfortunanely it still won't work my access token expired after 2 hours so I tried also this:

    public function getSimpleExtendedAccessToken(){

    $request = 'https://graph.facebook.com/oauth/access_token?
                client_id='. $this->getAppId(). 
                '&client_secret=' .$this->getApiSecret(). 
                '&grant_type=fb_exchange_token
                &fb_exchange_token=' .$this->getAccessToken();

    $response = file_get_contents($request);
    $params = null;
    parse_str($response, $params);
    return $params['access_token'];
}

and this one also expired after 2 hours. But when I checked what was inside the array $params in the second function there was written:

Array ( [access_token] => AAAFM5sO7[...] [expires] => 4897 )

(That part: "AAAFM5sO7[...]" is of course my access token number)

All access tokens which I received are the same: first one from getAccessToken(), second one from getExtendedAccessToken() and third one from getSimpleExtendedAccessToken().

Of course when I ask addtionaly more and more for extended access token the expiration number doesn't renew but it's counting down, and that is correct from the point of view of the Facebook documentation, because you can't ask for new access token each minute, but why I can't get 60 days access token?

Can anyone help me because I'm a bit cofused?

Community
  • 1
  • 1
Ziemo
  • 941
  • 8
  • 27
  • It might worth actually catching the error in your `try`/`catch` block, as that may hold the key. Save it to a text file; I believe the Facebook samples have you just simply do `error_log($e)`. – Martin Bean Apr 12 '12 at 10:04
  • I don't think so, because I received an access token from the first function `getExtendedAccessToken()`, not false as it is indicated in the error section of `try/cach`. Also second function `getSimplyExtendedAccessToken()` gives me valid access token - and this second function doesn't have `try/cach` section. – Ziemo Apr 12 '12 at 10:18
  • ...so nobody know how solve this problem?? – Ziemo Apr 12 '12 at 19:18
  • Jesus, give it more than a day! – Martin Bean Apr 12 '12 at 19:19

1 Answers1

1

I have a partial answer for my own question. I tryed to received extended access token of my own (and I'm the owner of the app) so Facebook doesn't gave me extended token, but when I tryed with some other I receive regular 60 days access token.

Ziemo
  • 941
  • 8
  • 27