I'm stuck trying to publish in a facebook page using the SDK in PHP 5.5. I want to make a php script to auto publish updates in a facebook page that I administer. When I make the request using the Graph API Explorer it works perfect, but I always receive the error message "(#200) The user hasn't authorized the application to perform this action" when doing from the PHP script.
These are the steps and code I'm using:
- Get a short-lived access token for my application using the "Get Access Token" option of the Graph API Explorer, with permissions manage_pages and publish_pages.
- Get a never expire access token using the getOAuth2Client of the PHP SDK.
- Get the page access token with the "/me/accounts" request
- Send the publish request using the page access token and method "PORT /pageId/feed" with params: access_token and message
After getting the page access token, if I do the publish request using the Graph API Explorer it works, but from the PHP script it fails.
Here is the complete code I'm using:
<?php
require_once __DIR__ . '/Facebook/autoload.php';
$appId = 'my_app_id';
$appSecret = 'my_app_secret';
$pageId = 'my_page_id';
$shortToken = 'token got in step 1';
$fb = new Facebook\Facebook([
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
]);
$oAuth2Client = $fb->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($shortToken);
echo '<p>Long-live token: ' . $longLivedAccessToken . '</p>';
// Get page access_token
$request = $fb->request('GET', '/me/accounts');
$request->setParams(['access_token' => $longLivedAccessToken]);
try {
$response = $fb->getClient()->sendRequest($request);
} 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;
}
$graphEdge = $response->getGraphEdge();
foreach ($graphEdge as $graphNode) {
if ($graphNode['id'] == $pageId) {
$pageAccessToken = $graphNode['access_token'];
}
}
echo '<p>Page access token: ' . $pageAccessToken . '</p>';
$request = $fb->request('POST', '/' . $pageId . '/feed');
$request->setParams(['access_token' => $pageAccessToken, 'message' => 'Page publish test message']);
try {
$response = $fb->getClient()->sendRequest($request);
} 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;
}
?>
An output example:
Long-live token: XXXX Page access token: XXX Graph returned an error: (#200) The user hasn't authorized the application to perform this action
All the tokens are valid, checked using the Facebook Access Token Debugger. The page token info contains the app id, page id and my user id, with the manage_pages, publish_pages and public_profile permissions.
My application has default settings. I'm just added a platform of type "Website" with the URL of my webpage. The app is "live" and not submitted for approval (I've just create the application to auto publish messages in a Facebook page).
I've read a lot of messages and examples here and in Internet but I do not get it to work. I hope anyone can help me.
Thank you very much!