在facebook js sdk app development ..我需要找到从收件箱发送的facebook消息的数量。我正在使用的代码是
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(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.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
// 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 checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '289533237896176',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.0' // use version 2.0
});
};
(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'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me/inbox',function(response) { for (var i=0;i<response.data.length;i++) {
var thread = response.data[i];
for (var j=0;j<thread.comments.data.length;j++) {
var comment = thread.comments.data[j].message;
console.log(comment);
}
}
}
);
}
</script>
<fb:login-button scope="public_profile,email,read_mailbox" onlogin="checkLoginState();">
<div id="status">
<div id="fb-root"></div>
</div>
</body>
</html>
这个脚本成功检索了前10条消息的数量..我需要的是显示总共的消息数量。
答案 0 :(得分:0)
Facebook自动将分页应用于返回项目列表的所有API请求。如果未指定页面大小,FB将应用其默认大小。从您的示例看,每页看起来有10个项目。
要移至下一页,您应使用response.paging.next
字段 - 它包含由FB生成的准备使用的URL以访问下一页。如果您到达最后一页,response.paging
字段将为空。
More reading about paging from FB specification
此外,如果您想将页面大小调整为例如每页50个项目,您可以limit=50
添加到您的请求中。代码如下:
FB.api('/me/inbox?limit=50', ...