我正在使用Google的Blogger API从我的Blogger博客中检索项目以显示在我的主页上。我正在使用Javascript made available on the Blogger Developer's Blog。我希望能够做的是过滤标签返回的条目。我已经做了很多搜索,但即使看起来这很简单,我还没有找到明确的方向。
我检索Blogger条目的代码如下所示:
function init() {
// Get your API key from http://code.google.com/apis/console
gapi.client.setApiKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
// Load the Blogger JSON API
gapi.client.load('blogger', 'v3', function() {
// Load the list of posts for code.blogger.com
var request = gapi.client.blogger.posts.list({
'blogId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fields': 'items(content,title,updated,id,labels)',
'fetchBodies': true
});
request.execute(function(response) {
var blogger = document.getElementById("blogger");
var anchor = 0;
for (var i = 0; i < response.items.length; i++)
{
var bloggerDiv = document.createElement("div");
bloggerDiv.id = "blogger-" + i;
bloggerDiv.className = "bloggerItem";
// For the parts of the response, have a look at the result at:
// http://code.google.com/apis/explorer/#_s=blogger&_v=v2&_m=posts.list&blogId=xxxxxxxxxxxxx
$(bloggerDiv).append("<h2>" + response.items[i].title + "</h2>");
var date = response.items[i].updated;
date = date.replace("T", " ");
date = date.replace("+03:00", "");
var printDate = new moment(date);
$(bloggerDiv).append("<p><span class='byline'>" + printDate.format('dddd, MMMM Do YYYY, h:mm:ss a') + "</span></p>");
$(bloggerDiv).append(response.items[i].content);
// This logic might be useful to keep around if I decide to do
// something different based on whether or not the last item
// needs different formatting in some way.
// if (i+1<response.items.length) {
// $(bloggerDiv).append("<hr>");
// }
// $(bloggerDiv).append("<hr>");
bloggerAnchor = document.createElement("a");
bloggerAnchor.name = "blogger-" + response.items[i].id;
blogger.appendChild(bloggerAnchor);
blogger.appendChild(bloggerDiv);
anchor = anchor + 1;
}
// find out which anchor the user wanted...
var hashVal = window.location.hash.substr(1);
// ... then jump to that position:
location.hash = "#" + hashVal;
});
});
}
这将返回博客上的所有内容。如何让它只返回label="XYZ"
?
我认为现在会回答这个问题,但似乎相关的the one question I found on SO包含现在已经死亡的链接,因此他们所持有的任何信息现在都不可用。
答案 0 :(得分:2)
我所要做的就是将这行代码添加到请求部分:
var request = gapi.client.blogger.posts.list({
'blogId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fields': 'items(content,title,updated,id,labels)',
'labels': 'XYZ',
'fetchBodies': true
});
绊倒我的一些事情是,这个领域是“标签”,“复数”,“不是标签”,“单数”。此外,出于某种原因,似乎&#39;标签&#39;规范必须在fetchBodies&#39;之前,或者代码失败。我不确定为什么会这样,但那是我的经历。