Facebook Graph API可以回复“我的商家”页面上的评论

时间:2019-02-21 13:12:31

标签: php facebook-graph-api

我拥有我的FB应用程序的这些权限。

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

我能够通过graph API在我的商家页面上获取评论。我一直在寻找如何使用API​​回复评论的方法。假设某人A审查了我的公司页面并留下了评论。所以我想通过API答复“个人身份”审查。 我已经尝试了以下方法,但是它们都不起作用。

CURL方法:

$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);

我还尝试了Graph API方法:

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);

以上都不起作用。我收到以下错误。

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

)

有人可以引导我吗?

1 个答案:

答案 0 :(得分:0)

是因为使用了use access_token。您需要使用页面访问令牌

1。新客户

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

2。获取访问令牌

$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。获取/我

// 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。获取每个页面的页面和access_token

// 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 (is_array($body['data']) && count($body['data'])) {
    // do something with your pages (save data in database)
}

6。发表回复

// 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();