Simple blogger widget to get list of posts

In 2014 I wrote how to customize a widget code published by someone else. It has been 6 years and that script throws some console errors, also it is based on Blogger API v1 so it now a bit out-dated. Currently there is a new and simple API v3

Please refer to the official Google APIs documentation API parameters and some examples and information about authentication.

I tried the callback parameter but for some reasons it does not work all the the times. Maybe callback is part of the old protocol? How knows. Currently I call the API using fetch and without callback it is basically the same number of lines of code.

<div class='pane-mini-archive' id='pane-mini-more-posts'>
</div>
<script type='text/javascript'>
  fetch('https://www.googleapis.com/blogger/v3/blogs/YOUR-BLOG-ID-HERE/posts?key=YOUR-KEY-HERE&fields=kind,items(title,published,url)&maxResults=50')
  .then(function(response) {
    return response.json()
  }).then(function(json) {
    var itemsString = json.items.reduce(function(acc, item) {
        return acc + '<li><a href="' + item.url + '">' + item.title + '</a></li>';
    }, '<ul id="archive-list">') + '</ul>';
    document.getElementById('pane-mini-more-posts').innerHTML = itemsString;
  }).catch(function(ex) {
    console.log('parsing failed', ex)
  });


Improves from old approach

  1. maxResults parameter gives me exactly the number of posts I need.
  2. items parameter gives me exactly the parameters I need, no more no less. So response size is definitely reduced.

Hope it helps

0 comments :

This work is licensed under BSD Zero Clause License | nacho4d ®