【问题标题】:Facebook Graph API to reply to a review on my Business pageFacebook Graph API 用于回复我的业务页面上的评论
【发布时间】:2019-07-15 10:03:29
【问题描述】:

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

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

我能够通过图形 API 在我的业务页面上获取评论。我一直在寻找如何使用 API 回复评论。假设人 A 审查了我的业务页面并留下了评论。所以我想通过API回复Person As review。 我尝试了以下方法,但没有一个有效。

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
        )

)

有人可以指导我吗?

【问题讨论】:

  • 您必须拥有足够的权限,并且您的应用程序必须经过 Facebook 验证。
  • @KubiRoazon 我上面提到的所有权限都已经被FB批准了。
  • @KubiRoazhon 这是正确的{fb_user_id}_{fb_comment_id}/comments??
  • 但是当你看到消息时,显然是权限问题。我没有在您的查询中看到页面 ID。
  • @KubiRoazhon 我搞定了。我在请求用户许可时忘记在权限数组中添加publish_pages

标签: php facebook-graph-api


【解决方案1】:

是因为你使用了User 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多