Learn More

Thursday, April 12, 2012

Basic Using the JavaScript Client Library

basic javascript api logo useful for create a widget
This post is to help someone in understanding javascript in google data. You will know how to use the JavaScript client library to send Google Data API queries and interpret returned responses. With this API, you can make a request and send them to a service, and receive responses. We assume that you are understand with Javascript programming. We also recommend you to read our post about Basic Google Data and Basic Blogger Api. And don't forget to learn more about basic idea of Google Data API's Protocol.


In Google Javascript APIs, there are a lot of classes to represent the elements used by the Google Data APIs. As example, in blogger there is a method to request cooments with feed, that is google.gdata.blogger.BlogCommentFeed(<Object> opt_params). But, if you want let someone to comments on your posts, then you can use google.gdata.blogger.CommentEntry(<Object> opt_params).

But, if you are new in Javascript, my suggestion is read continuation passing style. Because after you using classes of Google Javascript APIs, then you need to using continuation passing style. Anything that your client does to the returned data should be done in the continuation function, or called from that function. You may need to make some variables global in order to use them in multiple functions.

Google Javascript APIs are support in almost any browsers. The JavaScript client library allows your client to send Google Data requests from any domain while remaining compliant with the browser security model.

In practice, before loading the library of Google Javascript APIs, the client has to request the client library code from the server. It start with add the code below after <head>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

But, if your blog or application is not public, then you must using authentication. There are two ways at preloading the library. First, without using google.load(), in here you can repair the code above with

<script type="text/javascript" src="http://www.google.com/jsapi?autoload={modules:[{name:gdata,version:2.x,packages:[blogger,contacts]}]}"></script>.

this is more simple in roundtrips and latency. Second, with using google.load(). In <script type="text/javascript" src="http://www.google.com/jsapi"></script>, you must add the code below

google.load("gdata", "2.x", {packages: ["blogger", "contacts"]});

After you've called google.load(), you have to tell the loader to wait until the page finishes loading and then call your code:

google.setOnLoadCallback(getMyFeed);

Where getMyFeed() is a function defined in the next section of this document.

Now, if you are using non authentic, what you need first is call getMyFeed().  Create a connection to getMyFeed() with setupMyService() function. Then, call the library into getMyFeed(). See the example below

var feedUri = 'http://www.blogger.com/feeds/4999557720148026925/posts/default';

function setupMyService() {
var bloggerService = new google.gdata.blogger.BloggerService('com.appspot.interactivesampler');
  return bloggerService;
}

function getMyFeed() {
bloggerService= setupMyService();

bloggerService.getBlogCommentFeed(commentsFeedUri, handleCommentFeed, handleError);
}

Yes, you must create a function to for handle comment feed and to handle error when the library is not supported by browser. See the example below

function handleCommentFeed(myResultsFeedRoot) {
  alert("This feed's title is: " + myResultsFeedRoot.feed.getTitle().getText());
}

function handleError(e) {
  alert("There was an error!");
  alert(e.cause ? e.cause.statusText : e.message);
}

If you want need authentic, my suggestion is read using javascript client library from google. For now, we only provide tutorials for public blogger.



Recommended Books:


Post a Comment