I'm trying to create a automatic post script to fanpage using php SDK v5 and Graph API 2.4 !
I created two scripts:
1 - To get the list of page access tokens using publish_actions and manage_pages permissions. (returning ok)
2 - Offline publish to fanpage. (fail)
1) The first script works fine:
<?php
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
// config APP_ID e SECRET
define('APP_ID', 'xxxxxxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');
// instancia
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4'
]);
if(isset($_SESSION['fb_access_token'])) {
$accessToken = $_SESSION['fb_access_token'];
} else {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
if(isset($accessToken)) {
$oAuth2Client = $fb->getOAuth2Client();
// longlived access token
if (!$accessToken->isLongLived()) {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
}
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
if(isset($accessToken)) {
// Logged in!
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me/accounts', $accessToken);
echo '<pre>';
print_r($response->getDecodedBody());
echo '</pre>';
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
} else {
$helper = $fb->getRedirectLoginHelper();
$redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$loginUrl = $helper->getLoginUrl($redirect_url, [ 'manage_pages', 'publish_actions' ]);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}
2) The second script fails: (the script should work even if I am disconnected from facebook):
<?php
session_start();
// autoloader
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
define('APP_ID', 'xxxxxxxxxxxxxxx');
define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');
$page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// instance
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => 'http://www.example.com',
'message' => 'User provided message',
];
try {
$response = $fb->post('/me/feed', $linkData, $page_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
I`m getting the error:
Graph returned an error: Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.
In old graph api versions with php sdk v3, it works in a similar way...
What's wrong ?