Step 1: Get Your API Key and Blog ID
API Key: You can obtain an API key by creating a project in the Google Cloud Console.
Blog ID: You can find your Blogger Blog ID by going to your Blogger dashboard and checking the URL. It should look like this: blogger.com/blog/posts/1234567890123456789, where the long number is your Blog ID.
Step 2: Use Blogger API to Fetch Labels
The Blogger API v3 provides a way to fetch posts and extract labels. You'll need to use JavaScript (or any other language) to fetch the labels via the API.
Here's the HTML and JavaScript example that fetches labels using an API key and Blogger ID:
<!DOCTYPE html>
<html>
<head>
<title>Fetch Blogger Labels</title>
</head>
<body>
<h2>All Labels from Blogger Blog</h2>
<ul id="labelsList"></ul>
<script>
const apiKey = 'REPLACE_WITH_YOUR_API_KEY';
const blogId = 'REPLACE_WITH_YOUR_BLOG_ID';
const labelsSet = new Set();
const labelsList = document.getElementById('labelsList');
async function fetchAllPosts(pageToken = '') {
const url = `https://www.googleapis.com/blogger/v3/blogs/${blogId}/posts?key=${apiKey}&maxResults=50${pageToken ? `&pageToken=${pageToken}` : ''}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.items) {
data.items.forEach(post => {
if (post.labels) {
post.labels.forEach(label => labelsSet.add(label));
}
});
}
if (data.nextPageToken) {
await fetchAllPosts(data.nextPageToken);
} else {
displayLabels();
}
} catch (err) {
console.error("Error fetching posts:", err);
labelsList.innerHTML = '<li style="color:red;">Error fetching labels. Check blog ID and API key.</li>';
}
}
function displayLabels() {
const sortedLabels = Array.from(labelsSet).sort();
if (sortedLabels.length === 0) {
labelsList.innerHTML = '<li>No labels found.</li>';
} else {
sortedLabels.forEach(label => {
const li = document.createElement('li');
li.textContent = label;
labelsList.appendChild(li);
});
}
}
fetchAllPosts();
</script>
</body>
</html>
How It Works:
1. HTML Structure: Similar to the previous example, there's a ul element where the labels will be displayed.
2. API Fetching:
fetchBlogPosts() fetches posts from Blogger using the provided API key and Blog ID. It fetches up to maxResults (which can be 500 posts at a time).
If there are more posts, it uses pageToken to get the next page of results.
3. Label Extraction:
extractLabels() collects unique labels from all the posts and ensures no duplicates are added.
4. Display: Labels are displayed inside the ul as list items (li).
Usage:
1. Replace YOUR_API_KEY with your actual API key from Google Cloud.
2. Replace YOUR_BLOG_ID with your actual Blogger Blog ID.
3. Place this code in a custom HTML/JavaScript widget on your Blogger page.
Notes:
You may need to enable the Blogger API in your Google Cloud project.
The API fetches up to 500 posts at a time; if your blog has more, the script will continue fetching until all posts are retrieved.
This method ensures you're using the Blogger API directly to fetch your posts and labels efficiently.
0 Comments