I am trying to pull back an array of customer reviews (ratings?) from a particular facebook page.
I understand I need the manage_pages
permission to do this, however according to the facebook docs I need to submit my app for review, providing the steps needed to reproduce the use of this permission, along with a screencast of the permission in use.
The problem I'm having is that I cannot use the permission to provide the steps and screencast because any attempt to do so results in the API telling me that it needs that permission. So a bit of a paradox really.
I assume there must be some kind of way to reproduce the steps needed in a test environment, but setting up a test app and using the app_id
and app_secret
from the test app still resulted in the following error:
Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookAuthorizationException' with message '(#200) Requires manage_pages permission to manage the object'
My code is as follows:
<?php
$fb = new Facebook\Facebook([
'app_id' => $test_app_id,
'app_secret' => $test_app_secret,
'default_graph_version' => 'v2.8',
]);
if (isset($_GET['code'])) {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
}
}
if (!isset($_SESSION['facebook_access_token'])) {
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages'];
$loginUrl = $helper->getLoginUrl($site_url.'/fb-test.php', $permissions);
echo '<a href="' . $loginUrl . '" target="_blank">Log in with Facebook!</a>';
exit;
}
$access_token = $_SESSION['facebook_access_token'];
$response = $fb->get('/'.$page_id.'?fields=access_token', $access_token);
$page_access_token = $response->getDecodedBody()['access_token'];
$ratings = $fb->get('/'.$page_id.'/ratings', $page_access_token);
var_dump($ratings);
Edit (following comment from luschn)
I have altered my code to include the following, but I still get told that manage_pages
permission is required.
<?php
$response = $fb->sendRequest(
'GET',
'/'.$page_id.'?fields=access_token',
array (
'user' => $user_id,
'role' => 'administrators',
'state' => rand(0,9999999)
),
$access_token
);
$page_access_token = $response->getDecodedBody()['access_token'];
$response = $fb->sendRequest(
'GET',
'/'.$page_id.'/ratings',
array (
'user' => $user_id,
'role' => 'administrators',
),
$page_access_token
);
echo '<pre>'.print_r($response,true).'</pre>';
exit;