I have posted this question in Facebook Developer Group but no-one could give a compete answer. I have a Facebook application which uses server side authentication. The code looks like this:
$config = array();
$config['appId'] = $fbconfig['appid'];
$config['secret'] = $fbconfig['secret'];
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
//Facebook Authentication part
$mobile = false;
$code = false;
if (isset($_GET["code"]) && !empty($_GET["code"]) && strlen($_GET["code"])>1){
$code = trim($_GET["code"]);
}
//MOB VAR
if (isset($_REQUEST['mob']) && !empty($_REQUEST['mob']))
{
mobile = true;
}else{
}
if ($mobile){
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => $fbconfig['baseUrl'].$loginpart,
'scope' => 'email,user_likes'
)
);
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".APP_ID."&redirect_uri=".urlencode($fbconfig['baseUrl'].$loginpart)."&client_secret=".$fbconfig['secret']."&code=".$code;
}else{
$loginUrl = $facebook->getLoginUrl(
array(
'redirect_uri' => $fbconfig['appBaseUrl'].$loginpart,
'scope' => 'email,user_likes'
)
);
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".APP_ID."&redirect_uri=".urlencode($fbconfig['appBaseUrl'].$loginpart)."&client_secret=".$fbconfig['secret']."&code=".$code;
}
if ((!isset($_GET['code']) || empty($_GET['code']) ) ) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
} else{
if ($code){
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $sessionKey = $AccessToken = $params['access_token'];
if(isset($AccessToken) && !empty($AccessToken)){
if(isset($params['expires'])){
$ExpDate = $params['expires'];
I shortened the code not to annoy You.
THE PROBLEM
For some reason the code returns short $ExpDate
which can be from 3000 seconds to 7000 seconds. This happens not for all users but to 10%-15% of them.
What I have tried
- Despite the fact Facebook should return long-living access_token I
tried to exchange it with the
/oauth/access_token?
url. No result:it returns the same expire time. - I tried to catch the
$SERVER['HTTP_USER_AGENT']
to find out what in common do the users have with short-living access_tokens. No result:everything is different(they can be from mobile device, desktop, IOS native Facebook app....) - I changed my settings from privacy settings to every possible version, installed, removed application many times trying to reproduce the situation. No result: for me it works perfectly.
- I saved the code of the user and tried to get the access_token manually. I was not able to do that as I had forgotten that the codes can be used only one time.
How can You Guys Help
- If someone has the same authentication method please check your database. Do you have the same problem?
- If someone has an Idea why could this happen please help to find the reason.
- If we find out that this is a bug we can create a bug report on Facebook
Thank you.