doubutao6216 2017-02-23 10:58
浏览 55

如何通过Facebook Graph API检索页面评论/评级?

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;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算