I have a fully functional FQL query, but when it is triggered for the second time i get an error code 102: Requires user session
On my app i have an Autocomplete Search for Friends. I am using jquery ui autocomplete with a dynamic source using AJAX.
This is my PHP Function:
function fb_buscarAmigosAutocomplete($term='',$app_user=false){
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$term=trim(strtolower($term));
//Buscar usuarios
try{
$fql = " SELECT uid,name,first_name,last_name,pic_square
FROM user
WHERE uid IN (
SELECT uid2
FROM friend
WHERE uid1=me()
)
AND strpos(lower(name),'".$term."') >=0
";
if($app_user)
$fql .= " AND is_app_user";
$fql .= " LIMIT 5";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$result = $facebook->api($param);
} catch(FacebookApiException $e){
$error=$e->getMessage();
var_dump($e);
}
if(!empty($result)) {
$return['amigos']=$result;
$return['valido']=true;
}else
$return['valido']=false;
return $return;
}
The weird thing is that this was working fine for the last 2-3 weeks, and all of a sudden it stopped. Even "weirder", it still works the first time it is triggered, but not the second time. Since this is asynchronous, i dont understand what difference does it make if is the first, second, third, fourth time it is triggered..
Any ideas?
Edit 1 With further research i think i got a workaround. This may be because a bug reported here ans discused here
My workaround was to catch the first access_token i get with the Object Facebook, then attach it to every single instance of the Class, something like this
First connection:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$uid = $facebook->getUser();
if($uid){
$_SESSION['access_token']=$facebook->getAccessToken();
}
Any other use of the api:
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret'=> APP_SECRET
));
$facebook->setAccessToken($_SESSION['access_token']);
try{
$user_info = $facebook->api('/'.$uid.'?fields=id,name,first_name,last_name,email,picture');
} catch(FacebookApiException $e){
$error = $e->getMessage();
}
So far it has solved the problem
Edit 2
it did not... sometimes my access token is a valid alphanumeric +100 characters String and those times it works.
Sometimes it is a 48 only numbers String, and it doesn't... i'm really stucked here
The reason this is likely happening is because of the December 5, 2012 "Breaking Changes" that Facebook has rolled out. Specifically:
This is why your first request works, your second doesn't. On the 2nd attempt, the SDK attempts to exchange your the user's auth code for an access token. But due to these changes, this can only be done once. Your code looks like it should be working properly, since you are explicitly saving and then setting the
access_token
after you first receive it. Make sure that that is the case (confirm that the $_SESSION var is being saved/retrieved properly). If for some reason it is not, the 102 error is what you will see.Cheers