FQL Error 102 Requires user session on a repeated

2019-02-07 13:06发布

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

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-07 14:07

The reason this is likely happening is because of the December 5, 2012 "Breaking Changes" that Facebook has rolled out. Specifically:

New security restrictions for OAuth authorization codes

We will only allow authorization codes to be exchanged for access tokens once and will require that they be exchanged for an access token within 10 minutes of their creation. This is in line with the OAuth 2.0 Spec which from the start has stated that "authorization codes MUST be short lived and single use". For more information, check out our Authentication documentation.

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

查看更多
登录 后发表回答