php – Facebook Graph API to reply to a review on my Business page-ThrowExceptions

Exception or error:

I have these permissions for my FB App.

`email`
`default`
`publish_pages`
`manage_pages`

I was able to fetch the reviews on my Business Page through the graph API. I am stuck in finding out how to reply to a review using the API. Suppose Person A reviewed my Business page and left a review. So I want to reply to Person As review through API.
I have tried the following but none of them is working.

CURL method:

$chu = curl_init();
curl_setopt($chu, CURLOPT_URL, "https://graph.facebook.com/v3.2/{fb_user_id}_{fb_comment_id}/comments?message=Thanks!!&access_token={access_token}");
curl_setopt($chu, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chu, CURLOPT_POST, 1);
curl_setopt($chu, CURLOPT_TIMEOUT, 50);
$resultu = curl_exec($chu);
curl_close($chu);
$resultu = json_decode($resultu);
echo "<pre>";
print_r($resultu);

Also I have tried Graph API method:

include $_SERVER['DOCUMENT_ROOT']."/manager/include/contact-header.php";
require_once CORE_PATH.'manager/all_apis/php-graph-sdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
    'app_id'                => FB_APP_ID,
    'app_secret'            => FB_APP_SECRET,
    'default_graph_version' => FB_APP_API_VERSION,
]);
try {
  $response = $fb->post(
    '/{fb_user_id}_{fb_comment_id}/comments', // user_id is the ID of the user who made the page
    array (
      'message' => 'This is a test comment',
    ),
    '{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;
}
$graphNode = $response->getGraphNode();
echo "<pre>";
print_r($graphNode);

None of the above is working. I get the following error.

stdClass Object
(
    [error] => stdClass Object
        (
            [message] => (#200) You do not have sufficient permissions to perform this action
            [type] => OAuthException
            [code] => 200
            [fbtrace_id] => HvneY0sxX69
        )

)

Can someone please guide me?

How to solve:

Is because you use the Use access_token. You need to use Page access token

1. New client

$fb = new Facebook\Facebook([
    'app_id'                => FB_APP_ID,
    'app_secret'            => FB_APP_SECRET,
    'default_graph_version' => FB_APP_API_VERSION,
]);

2. Get access token

$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken( 'your_callback_url' );

$oAuth2Client = $fb->getOAuth2Client();

if (! $accessToken->isLongLived()) {
    // Exchanges a short-lived access token for a long-lived one
    try {
       $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
    } catch (Facebook\Exceptions\FacebookSDKException $e) {
       \\ Error message
    }
}

$facebook_oauth_token_value = $accessToken->getValue();

3. Get /me

// Build the request for the user 
$request = $fb->request("GET", "me");
$request->setAccessToken($facebook_oauth_token_value);
// Send request and store the response.
$response = $fb->getClient()->sendRequest($request);
// Fetch this user.
$body = $response->getDecodedBody();

4. Get Pages AND access_token for each page

// Build the request for the pages access tokens  
$request = $fb->request("GET", '/'.$body['id'].'/accounts?fields=name,access_token,id,link&access_token='.$facebook_oauth_token_value);
// Send request and store the response.
$response = $fb->getClient()->sendRequest($request);
// Fetch all the pages for this user.
$body = $response->getDecodedBody();

5. If pages save each page access token

if (is_array($body['data']) && count($body['data'])) {
    // do something with your pages (save data in database)
}

6. Post reply

// build the facebook POST request to post a reply to review.
$response = $fb->post('/' . $review_id.'/comments?',[
   'message' => $comment,
], $page_api_key);

$body = $response->getDecodedBody();

Leave a Reply

Your email address will not be published. Required fields are marked *