我正在使用图形API。
我有一个已登录的用户,并且想要获取该用户是其管理员的所有页面的页面ID列表。
有办法吗?该文档是非常糟糕的-和循环。
Answers:
使用Graph API很简单。脚步:
manage_pages
从用户那里获得许可(扩展许可)。您可以在图形浏览器中测试此过程->只需单击“扩展权限”下的“获取访问令牌”按钮->选中“ manage_pages”并提交。它将为您提供admin-page-details JSON。
我用一些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;
}}
FB.api(/me/accounts)
如果您不想使用FQL,可以致电。
“帐户”是User对象的连接。请参阅此文档@ http://developers.facebook.com/docs/reference/api/user
当然,有了Facebook,总是有一个陷阱。现在,此方法不仅将返回用户所管理的页面,还将返回他们已安装的应用程序。我几乎肯定这不是预期的行为-我似乎记得几个月前使用过此功能,只获得了页面列表。该文档也没有在此列表中提及应用程序。
不过,这是一个容易解决的问题-Facebook返回列表中每个项目的名称,类别和ID,并且每个应用程序都有一个“应用程序”类别。我只是确保只列出类别不是“应用程序”的项目。
您在登录时使用JavaScript SDK寻求许可
FB.login(function(){}, {perms:'manage_pages'});
然后,他们登录后即可按以下方式检索页面(和应用程序):
FB.api('/me/accounts', function(response){
console.log(response);
})
{perms: 'manage_pages'}
和之间有什么区别{scope: 'manage_pages'}
?
请注意,您的解决方案将返回Pages以及Applications。如果严格需要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)"
}
允许
$facebook->getLoginUrl( array( "scope" => "manage_pages" ) );
行动
$accounts = $facebook->api('/me/accounts');
return $accounts;
<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>
对于带有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"
]
}
不要在阵列中寻找“ ADMINISTER”,而要寻找“ MANAGE”。
此处的完整信息:https : //developers.facebook.com/docs/pages/access-tokens