Facebook:获取用户管理的页面列表

时间:2010-06-03 12:46:49

标签: facebook facebook-graph-api facebook-page

我正在使用图表api。

我有一个已登录的用户,并希望获取用户所管理的所有页面的页面ID列表。

有没有办法做到这一点?文档很糟糕 - 而且是循环的。

10 个答案:

答案 0 :(得分:130)

使用Graph API很简单。步骤进行:

  1. 获取用户的manage_pages权限(扩展权限)。
  2. 调用图表API - https://graph.facebook.com/me/accounts
  3. 您可以测试此过程in the graph explorer - >只需点击'获取访问令牌'按钮 - >在'扩展权限'下,选中'manage_pages'&提交它。它将为您提供admin-page-details JSON。

答案 1 :(得分:13)

我用一些FQL解决了它:

FB.api({method: 'fql.multiquery',
        access_token: <access_token>,
        queries: {
            query1: 'select page_id from page_admin where uid = ' + <uid>,
            query2: 'select page_id, name, page_url from page where page_id in (select page_id from #query1)'
        }
       }, function(queries){
           var pages = queries[1].fql_result_set;
       }}

答案 2 :(得分:11)

如果您不想使用FQL,可以致电FB.api(/me/accounts)

'accounts'是User对象的连接。请参阅此文档@ http://developers.facebook.com/docs/reference/api/user

当然,有了Facebook,总有一个问题。现在,此方法不仅会返回用户管理的页面,还会返回他们安装的应用程序。我几乎肯定这不是预期的行为 - 我似乎记得几个月前使用它并且只得到一个页面列表。该文档也未提及此列表中的应用程序。

这是一个容易解决的问题 - Facebook会返回列表中每个项目的名称,类别和ID,每个应用程序都有一个“应用程序”类别。我只是确保我只列出其类别不是“应用程序”的项目。

答案 3 :(得分:8)

转到此地址

  

https://developers.facebook.com/tools/explorer/431294226918345/?method=GET&path=me%2Faccounts%3Ftype%3Dpage`

只需点击获取访问令牌,然后转到扩展权限

选中 manage_pages 复选框

点击获取访问令牌

然后在FQL下写这个

  

我/帐户?类型=页

点击提交。并且您将获得登录用户admin的所有页面列表

答案 4 :(得分:7)

您在登录时使用JavaScript SDK请求权限

FB.login(function(){}, {perms:'manage_pages'});

然后一旦他们登录,您就可以检索页面(和应用程序),如下所示:

FB.api('/me/accounts', function(response){
    console.log(response);
})

答案 5 :(得分:5)

请注意,您的解决方案会返回页面以及应用程序。如果您严格要求Pages,则可以将FQL Multiquery与“Type not equals”子句一起使用,如下所示:

{
"query1":"select page_id from page_admin where uid = me()",
"query2":"select page_id, name, page_url, type from page where type!='APPLICATION' AND page_id in (select page_id from #query1)"
}

答案 6 :(得分:5)

<强>权限

$facebook->getLoginUrl( array( "scope" => "manage_pages" ) );

<强>动作

$accounts = $facebook->api('/me/accounts');  
return $accounts;

答案 7 :(得分:5)

您还可以使用&#34; pages_show_list&#34;权限,如果您只想要用户管理的Facebook页面列表。

&#34; manage_pages&#34;权限将要求用户管理他的页面的权限,这可能会过于干扰,具体取决于您的需要。

答案 8 :(得分:1)

<head>
 <link rel="stylesheet" href="@Url.Content("~/Content/jquery.remodal.css")">
</head>

<body>
<script type="text/javascript" src="@Url.Content("~/Scripts/Home/jquery.remodal.js")"></script>
<div class="remodal" id="page-selector-remodal" data-remodal-id="pageselector">
        <p>Please select a facebook page Share </p>
        <div id="page-name-container">
            <select id="page-name" class="form-control">
            </select>
        </div>
        <a class="remodal-confirm" id="facebookPageSelectSubmit" href="#">OK</a>
        <a class="remodal-cancel" id="remodal-cancel" href="#">CANCEL</a>
    </div>

    <div data-remodal-id="modal-status">
        <p id="modal-status-content">
            The Account you have selected does not have Email.
        </p>
        <br>
        <a class="remodal-confirm" href="#">OK</a>
    </div>
<script type="text/javascript>
 (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s);
            js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

        window.fbAsyncInit = function () {
            FB.init({
                appId: 'YOUR APP ID',
                cookie: true,  // enable cookies to allow the server to access 
                // the session
                xfbml: true,  // parse social plugins on this page
                version: 'v2.2' // use version 2.1
            });
        };

        var pageSelector = $('[data-remodal-id=pageselector]').remodal();
        var modalstatus = $('[data-remodal-id=modal-status]').remodal();

        function statusChangeCallback(response) {

            if (response.status === 'connected') {
                // Logged into your app and Facebook.
                //testAPI();
            } else if (response.status === 'not_authorized') {
                // The person is logged into Facebook, but not your app.
                $("#modal-status-content").empty().html(response.status);
                modalstatus.open();
            }

            else {
                $("#modal-status-content").empty().html(response.status);
                modalstatus.open();
                // The person is not logged into Facebook, so we're not sure if
                // they are logged into this app or not.
                document.getElementById('status').innerHTML = 'Please log ' +
                    'into Facebook.';
            }
        }


        function FacebookHandler() {
            FB.login(function (result) {
                if (result != null && result.authResponse != null && result.authResponse != undefined) {
                    facebookPageData = result;
                    FB.api('/me/accounts', function (accountsResult) {
                        if (accountsResult != null && accountsResult.data.length != 0) {
                            //open the remodal here
                            pageSelector.open();
                            facebookAccountsData = accountsResult;
                            var data = accountsResult['data'];
                            if (data != null) {
                                for (var i = 0; i < data.length; i++) {
                                    $("#page-name").append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
                                }
                            }
                            unblockUI('body');
                            $("#flip-container, #feature-container, #branding-container, #intro-arrow-container, #share-container, #copyright-text-container").hide();
                            $("body").css("padding-right", "0");
                        }
                        else {
                            $("#modal-status-content").empty().html("The Account you have selected does not have any facebook page,<br />Post to Wall.");
                            modalstatus.open();
                            pageSelector.open();
                            unblockUI('body');
                        }
                    });
                }
                else {
                    $("#modal-status-content").empty().html("Unable to retrieve your details from facebook, try again after sometime.");
                    modalstatus.open();
                    unblockUI('body');
                }
            }, { scope: 'manage_pages, publish_stream' });
        }

        $("#facebookPageSelectSubmit").on("click", function () {
            var facebookpageId = $("#page-name option:selected").val();
            if (facebookpageId != null) {
                FB.api('/' + facebookpageId, function (identity) {

                    if (identity != null) {
                        FB.api('/' + facebookpageId, { fields: 'access_token' }, function (resp) {
                            if (resp.access_token != null) {
                                //Get the "resp"(Data) here
                        }
                        else {

                        }

                    });
                }

                else {

                }
            });
        }
        else {

        }
    });
</script>

//Finally call the "FacebookHandler()" function on click


</body>

答案 9 :(得分:0)

对于带有Javascript的新GRAPH API v3,请使用“任务”字段而不是“权限”字段。

//Example JS Call
FB.api('/me/accounts?fields=name,picture.type(square),access_token,tasks', function(response) {console.log(response)});

//Example Response
{
      "name": "Engage",
      "picture": {
        "data": {
          "height": 50,
          "is_silhouette": false,
          "url": "https://scontent.xx.fbcdn.net/v/t1.0-1/c1.0.50.50a/p50x50/430597_259746387431503_2144341304_n.jpg?_nc_cat=103&_nc_eui2=AeGVrU8Wxe7k5BMvRXOEAcUo9dMIxyeMP9POPkYDwfgdRl8QquAtz1GcwXpJaK4z_0o&_nc_ht=scontent.xx&oh=e5b952a4adbbcd1b1af6b71b688f7284&oe=5CF9A64C",
          "width": 50
        }
      },
      "access_token": "XXXXXXXXXX",
      "id": "253263371413138",
      "tasks": [
        "ANALYZE",
        "ADVERTISE",
        "MODERATE",
        "CREATE_CONTENT",
        "MANAGE"
      ]
    }

查找“ MANAGE”,而不是查找数组中的“ ADMINISTER”。

此处提供完整信息:https://developers.facebook.com/docs/pages/access-tokens