Wednesday, May 11, 2011

Getting started with Google Books API

Google announced that their Books API would be available. So I thought I would walk through a sample using it.  What looked really promising is using the Client Side code, not needing any server script running to get the API up and running.   From the bottom of the https://code.google.com/apis/books/docs/v1/getting_started.html page there is a simple html page (it's broken, so here it is working).

<html>
  <head>
    <title>Books API Example</title>
  </head>
  <body>
    <div id="content"></div>
    <script>
      function handleResponse(response) {
      for (var i = 0; i < response.items.length; i++) {
        var item = response.items[i];
        // in production code, item.text should have the HTML entities escaped.
        document.getElementById("content").innerHTML += "<br>" + item.volumeInfo.title;
      }
    }
    </script>
    <script src="https://www.googleapis.com/books/v1/volumes?q=harry+potter&callback=handleResponse"></script>
  </body>
</html>

So, ok, great, now you can load that into a page and it will render a page that looks a little something like
OK, now lets try and do something a little more interesting with that.  I looked into the Embedded Viewer API  and using the first example as a guide, I made some simple modifications to our original code and added the ISBN number, and you can use something simple like

function initialize() {
        var viewer = new google.books.DefaultViewer(document.getElementById('viewerCanvas'));
        viewer.load('ISBN:0738531367');
      }
  
to load the preview of the book into the browser.

See the example here: http://lloyd.s3.amazonaws.com/code/booksapi/client2.html