Yuval
Yuval

Reputation: 514

How can facebook php sdk get the access token from javascript sdk?

FacebookI am using facebook javascript sdk to loigin a user, and then I wish to get some of his info via the php sdk, but I keep getting "An active access token must be used to query information about the current user."

this is the relevant html/js code:

     <script>


 window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : '8',                        // App ID from the app dashboard
      channelUrl : '//url/frontend/channel.php', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      cookie     : true,
      oauth:true,
      xfbml      : true                                  // Look for social plugins on the page
    });
  FB.Event.subscribe('auth.authResponseChange', function(response) {
 if (response.status === 'connected') {
      submitform();
}     });
  };

  function submitform() {

    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me?fields=email,name', function(response) {
      //submits a form from this page...
    });

  }
</script>
       <a href="javascript:FB.login()" class="facebook-login">Facebook</a>

and the php that fails is :

$facebook = new Facebook(array(               
                        'appId'  => Configuration::$facebook_app_id,
                          'secret' => Configuration::$facebook_app_secret));
            // Get User ID

             $user_profile = $facebook->api('/me?fields=name,id','GET');

It seemed to work for a while, and then it breaks with

"   [message] => An active access token must be used to query information about the current user.
                   [type] => OAuthException
                   [code] => 2500",

Anyone has an idea how to stabilize this?

Thanks

Upvotes: 3

Views: 2748

Answers (1)

Elephant
Elephant

Reputation: 1344

Maybe that happens because your acces token was expired? There are several access token types. And if your access token is a "short-term token" - you must refresh it. you can read abot acces tokens here https://developers.facebook.com/docs/facebook-login/access-tokens/ In python server lib for facebook i used method fetchToken(appId, secret) to refresh my expired token.

Upvotes: 2

Related Questions