ramo
ramo

Reputation: 363

Facebook access_token strange issues on website while requesting fb friends

i have an application written with symfony 2 (and using an fosfacebookbundle with custom userprovider from fosub). i use the js sdk and the php sdk from facebook in that applicatoin. log in into my app and surfing around is possible. I have one page where a i show a users facebook friends. in my backend i use /me/friends for that request. lets say i stay about an hour on that page that shows the friends without any activity. if i refresh the page after that hour, i get the message:

An exception has been thrown during the rendering of a template ("An active access token must be used to query information about the current user.") in 

that confuses me somehow. i still can visit the other pages of my website where no direct "facebook requests" are done. If i var_dump the facebook access_token on my other pages, they're looking different, e.g. if i'm logged in with facebook in the same browser and log in into my aplication via facebook, i get the following access_token on the page where i display the users friend: AAACbRz2shZAIBAFX66jLuQktdlzzIx52lnSvgZB5hiNNxvy0lPbKMcUsC1BHLTnaAllXI9WN7dRHfa5TOj4HmZBjdwgjP5In46dMYSGZBfWrj

facebook is opened in another tab in the same browser: if i log out now from facebook and refresh the page on my website where the users friends are shown, i get the above exception message. if i browse to another page, and var_dump the access_token, i get this one: string(48) "170732376699858|6c2b4d7ca20f8a607418845634356"

so, just the page where the users facebook friends are shown is not working properly.

on js site i'm doing the following:

    function onFbInit() {  

  if (typeof(FB) != 'undefined' && FB != null ) {

        FB.Event.subscribe('auth.authResponseChange', function(response) {
            $.each(reponse, function(e) {
                alert(e);
                });
            });

      FB.Event.subscribe('auth.statusChange', function(response) {
          if (response.session || response.authResponse) {
              setTimeout(goLogIn, 500);
          } else {
              window.location = "{{ path('_security_logout_fb') }}";
          }
      });
  } else  {
      setTimeout(goLogIn, 500);
      }
     }

any ideas what i'm doing wrong? Is there a way via php sdk to check if the access_token is valid? I read through the docs, but somehow i didn't find a way.

I'm not using the offline acces_token because it'll be obsolete

Thanks, Ramo

Upvotes: 1

Views: 852

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164367

This actually sounds exactly right. Since you don't use the offline_access permission (and you are right not to) the tokens that you are granted with by facebook per user expire after some time, as you described.

You have a few options to handle this issue, both at the server side and in the client side. But since you are making the api request at the server side you need to first find out if the access token you are about to use is valid, there's a good official blog post that explains how: http://developers.facebook.com/blog/post/500/

As for why you get an error page only on that page and not other pages in your site, it's probably because you don't try to catch an exception when making the facebook api request, which then returns an error, but since it's the only place you make that request, that's the only broken page.

Upvotes: 1

Related Questions