Facebook API,获取页面用户管理员(manage_pages)

时间:2015-08-15 12:23:34

标签: php facebook api

我正在使用PHP SDK制作基于Web的Facebook APP。

我正在尝试获取所有页面的列表,用户管理员,并将其显示给用户。

目前无法使用,即使我在测试应用中测试了所有内容,您也无需查看和接受扩展权限。

if (isset($session)) {
    // graph api request for user data
    $request = new FacebookRequest( $session, 'GET', '/me?fields=id,name,accounts'  );
    $response = $request->execute();
    //get response
    $graphObject = $response->getGraphObject();
    $name = $graphObject->getProperty('name');
    $id = $graphObject->getProperty('id');
    $email = $graphObject->getProperty('email');
    $accounts = $graphObject->getProperty('accounts');

    echo "Your name is: ";
    echo "$name.";
    echo "<br>$accounts";
}

非常感谢任何帮助

3 个答案:

答案 0 :(得分:2)

这很奇怪,但我找不到方法FacebookRequest-&gt; execute()...... 但是我发现了一个适合我的解决方案:

function GetPages()
{
    $token = 'CurrentUsersTokenIsHere';
    // Instantiates a new Facebook super-class object from SDK Facebook\Facebook
    $fb = new Facebook([
        'app_id'     => $fb_app_id,
        'app_secret' => $fb_app_secret
    ]);
    $response = $fb->get(
        '/me?fields=accounts,name,email',
        $token
    );
    $graphObject = $response->getDecodedBody();
    $fbUserName  = $graphObject['name'];
    $fbUserEmail = $graphObject['email'];
    $fbUserID    = $graphObject['id'];
    $pages       = $graphObject['accounts'];
    // In general there is an array and you can get each page like:
    $singlePage  = $pages['data']['0'];
    $pageName    = $singlePage['name'];
    $pageCategory= $singlePage['category'];
    $pageID = $singlePage['id'];
    return $pages;
}

还有一件事,让我们说理解, $ graphObject ['accounts'] 这是一个看起来像这样的数组:

array(
'data' => array (
        0 => array (
                  'access_token' => 'someToken',
                  'category'     => 'Some category',
                  'name'         => 'Some name',
                  'id'           => 'Some id',
                  'perms'        => array (
                                          0 => 'CREATE_ADS',
                                          1 => 'BASIC_ADMIN',
                                    ),
                 ),
        1 => ...
        ),
'paging' => array (
        'cursors' =>array (
                    'before' => 'Mzc4MDAwOTcyMjE4MTYw',
                    'after' => 'MTQ1MTM2NzgxNTE0MDkwMAZDZD',
                    ),
                  ),
);

答案 1 :(得分:0)

  1. 确保您拥有manage_pages权限
  2. 使用Graph API Explorer中的相同访问令牌尝试相同的调用,如果它不存在,那么它不是PHP SDK问题
  3. 尝试调试$response并查看其中的内容

答案 2 :(得分:0)

首先在版本v2.2及更高版本check it here中弃用<page_id>/admins端点但是如果您有v2.1则可以

$request = new FacebookRequest( $session, 'GET', '<your_page_id>/admins'  );
$response = $request->execute();
// Todo with $response

注意:请确保您将此请求与页面访问令牌一起发送。