Web Jo
Web Jo

Reputation: 107

FB SDK. The session is invalid because the user logged out

The code below works perfectly and posts to my Facebook page wall. But when I logged out from Facebook, it threw the following error:

Error validating access token: The session is invalid because the user logged out.

I tried to log in to Facebook again but it still shows the same error.

Any idea?? Thanks

<?php
require_once("facebook/src/facebook.php");
$pageid = 'mypageid';
$config = array();
$config['appId'] = 'myappid';
$config['secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$access_token = "my-permanent-page-token";
$fb = new Facebook($config);

$params = array(
"access_token" => $access_token,
"message" => "I'm a fb page",
"link" => "http://www.domainname.xyz",
"picture" => "http://www.domainname.xyz/1.png",
"name" => "I'm a fb page",
"caption" => "",
"description" => "I'm a fb page"
);

try{
    $ret = $fb->api('/' . $pageid . '/feed', 'POST', $params);
    echo 'Successfully posted to Facebook';
}
catch(Exception $e){
  echo $e->getMessage();
}


?>

Upvotes: 4

Views: 13144

Answers (1)

Ben
Ben

Reputation: 57207

You'll need a new access token. Hit this API:

https://graph.facebook.com/oauth/access_token?client_id=YOUR_ID&client_secret=YOUR_SECRET&grant_type=client_credentials

A few notes:

  • No need to be logged in

  • Your ID and secret are in the dashboard for your app

  • There's also quite a few third-party sites which generate the token for you. I would recommend against using this, seems like phishing to me - don't expose your secret.

Reference:

https://developers.facebook.com/docs/facebook-login/access-tokens

Upvotes: 2

Related Questions