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?