Vous êtes sur la page 1sur 18

Developer's Guide: JavaScript

The Blogger Data API allows client applications to view and update Blogger content in the form of Google Data API feeds. Your client application can use the Blogger Data API to create new blog posts, edit or delete existing blog posts, and query for blog posts that match particular criteria. In addition to providing some background on the capabilities of the Blogger Data API, this document provides examples of basic Data API interactions using the JavaScript client library. If you're interested in understanding more about the underlying protocol that the library uses, see the Protocol section of this developer's guide.

Contents
1. 2. 3. 4. Audience Terms of use About supported environments Getting started 1. Creating a Blogger account 2. Acquiring the library 5. Authenticating to the Blogger service 1. AuthSub proxy authentication 6. Interactive tutorial samples New!

Audience
This document is intended for programmers who want to write JavaScript client applications that can interact with Blogger. It provides a series of examples of basic Data API interactions using the JavaScript client library. For Blogger Data API reference information, see the Protocol reference guide. This document assumes that you understand the general ideas behind the Google Data APIs protocol and the data model and control flow used by the JavaScript client library. It also assumes that you know how to program in JavaScript. For reference information about the classes and methods provided by the client library, see the JavaScript client library API reference. This document is designed to be read in order; each example builds on earlier examples.

Terms of use
You agree to abide by the Google JavaScript Client Library Terms of Use when using the JavaScript client library.

About supported environments


Currently, we only support JavaScript client applications that run in a web page in a browser. Currently supported browsers are Firefox 1.5 and higher, and Internet Explorer 6.0 and higher. The JavaScript client library handles all communication with the service's server. If you're an experienced JS developer, you may be thinking, "But what about the same origin policy?" The JavaScript client library allows your client to send Google Data API requests from any domain while remaining compliant with the browser security model.

Getting started
Before you can write a JavaScript client application, you need to do some setup to acquire the library.

Creating a Blogger account


You may want to sign up for a Blogger account for testing purposes. Blogger uses Google Accounts, so if you already have a Google account, you're all set.

Acquiring the library


Before your client can use the client library, the client has to request the client library code from the server. Start by using a <script> tag in the <head> section of your HTML document to fetch the Google AJAX API loader:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

To acquire the Google Data API client library after fetching the loader, use the following line in your JavaScript setup code, which must be called from the <head> section of your HTML document (or from a JavaScript file that's included using a <script> tag in the <head> section of your HTML document):
google.load("gdata", "1.x");

The second parameter to google.load() is the requested version number of the JavaScript client library. Our version numbering scheme is modeled after the one used by the Google Maps API. Here are the possible version numbers and what they mean:
"1"

The second-to-last revision of major version 1.


"1.x"

The very latest revision of major version 1.


"1.s"

The latest stable revision of major version 1. We will occasionally declare a certain version of the client library to be "stable," based on feedback we

receive from developers. However, that version may not have the latest features. "1.0", "1.1", etc A specific version of the library, with a specified major and minor revision number. 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(getMyBlogFeed);

Where getMyBlogFeed() is a function that we'll define in a later section of this document. Use this approach instead of having an onload handler attached to the <body> element.

Authenticating to the Blogger service


You can access both public and private feeds using the Blogger Data API. Public feeds don't require any authentication, but they are read-only. If you want to modify blogs, then your client needs to authenticate before requesting private feeds. The JavaScript client library uses the AuthSub authentication system. For more information about authentication with Google Data APIs in general, see the authentication documentation.

AuthSub proxy authentication


AuthSub proxy authentication is used by web applications that need to authenticate their users to Google Accounts. The website operator and the client code don't have access to the username and password for the Blogger user; instead, the client obtains special AuthSub tokens that allow the client to act on a particular user's behalf. Here's a brief overview of what happens during the authentication process for a webbased JavaScript client: 1. The client application calls the google.accounts.user.login() method provided by the client library, passing it a "scope" value that indicates which Google service to use. For Blogger, the scope is "http://www.blogger.com/feeds". 2. The client library sends the browser to Google's "Access Request" page, where the user can enter their credentials to log in to the service. 3. If the user logs in successfully, then the AuthSub system sends the browser back to the web client's URL, passing along the authentication token. 4. The JavaScript client library stores the token in a cookie and returns control to the client application's function that called google.accounts.user.login(). 5. When the client application subsequently calls client library methods that interact with Blogger, the client library automatically attaches the token to all requests. Note: For the JavaScript client library to make authenticated Blogger requests in a web browser, your page must contain an image that's hosted at the same domain as your page. It can be any image, even a single-pixel transparent image, but there must

be an image on the page. If you want the image to not appear on your page, you can use the style attribute of the <img> tag to position the image outside the rendering area. For example: style="position:absolute; top: -1000px;" Here's the client-application code that handles logging in. We'll call the setupMyService() function from other code later.
function logMeIn() { scope = "http://www.blogger.com/feeds"; var token = google.accounts.user.login(scope); } function setupMyService() { var myService = new google.gdata.blogger.BloggerService('exampleCo-exampleApp1'); logMeIn(); return myService; }

Tip: We strongly recommend that you provide a login button or other user input mechanism to prompt the user to start the login process manually. If, instead, you call google.accounts.user.login() immediately after loading, without waiting for user interaction, then the first thing the user sees on arrival at your page is a Google login page. If the user decides not to log in, then Google does not direct them back to your page; so from the user's point of view, they tried to visit your page but were sent away and never sent back. This scenario may be confusing and frustrating to users. In the example code in this document, we'll be calling google.accounts.user.login() immediately after loading, to keep the example simple, but we don't recommend this approach for real-world client applications. Note that you don't have to do anything with the variable named token; the client library keeps track of the token, so you don't have to. Note: When you create a new BloggerService object, the client library calls a method named google.gdata.client.init(), which checks that the browser the client is running in is supported. If there's an error, then the client library displays an error message to the user. If you want to handle this sort of error yourself, then you can explicitly call google.gdata.client.init(handleInitError) before you create the service, where handleInitError() is your function. If an init error occurs, then your function receives a standard Error object; you can do whatever you want with that object. The token remains valid until you revoke it by calling google.accounts.user.logout():
function logMeOut() { google.accounts.user.logout(); }

If you don't call logout(), then the cookie that stores the token lasts for two years, unless the user deletes it. The cookie is retained across browser sessions, so the user can close their browser and then reopen it and come back to your client and they'll still be logged in.

However, there are certain unusual circumstances in which a token can become invalid during a session. If Blogger rejects a token, your client should handle the error condition by calling logout() to remove the cookie containing the current token, and then calling login() again to acquire a new, valid token. There are two other AuthSub methods that you may find useful in various contexts:
google.accounts.user.checkLogin(scope) tells you whether or not the

browser currently has an authentication token for the given scope. google.accounts.user.getInfo() provides detailed information about the current token, for debugging use. For details about using JavaScript to interact with AuthSub, including information on token management and on checkLogin() and getInfo(), see the Using "AuthSub" Authentication with the JavaScript Client Library document. Back to top

Interactive tutorial samples


Introduction to interactive samples
One of the advantages of having a JavaScript client library is that it allows us to provide an interactive browser environment to render sample codes. The ability to play with the code as you are going through sample code is a great way to learn a new client library. The following samples are completely interactive, you are able to modify and execute the code straight out of the source code textarea. Feel free to modify the source code to get a feel for how the different objects and properties are used. You can utilize the predefined PRINT() method which outputs the argument as a string in the output tab after you hit the "Run" button. Caution: The samples below are structured to minimize the accidental update or deletion of your personal Blogger data. Any data inserted into your blog(s) is prefixed with "JS-Blogger-Client". In this way, the samples will prevent you from accidently updating or deleting a real post or comment in your blog. But caution should always be exercised, as you are able to change the code any which way you desire.

Managing Blogs: Retrieve a list of blogs

Managing Blog Posts: Retrieve a list of blog posts Creating a new blog post Retrieve a specific blog post Update a blog post Delete a blog post Query for blog posts

Managing Post Comments: Retrieve a list of comments Create a comment Delete a comment

Retrieve a list of blogs


// Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default "metafeed" feed is used to retrieve a list of blogs for a particular loggedin user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method invoked when getBlogFeed() returns feed data var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); // Has the user created any blogs? if(!blogEntries.length) { PRINT('First <a href="http://www.blogger.com" target="_blank">create a blog</a>.'); return; } for (var i = 0, blogEntry; blogEntry = blogEntries[i]; i++) { var blogTitle = blogEntry.getTitle().getText(); var blogURL = blogEntry.getHtmlLink().getHref(); var blogUpdated = blogEntry.getUpdated().getValue().getDate(); PRINT('Blog title: <b><a href="' + blogURL + '" target="_blank">' + blogTitle + '</a></b>'); PRINT('<li><small>Last updated: ' + blogUpdated + '</small></li>'); } }; // Error handler called when getBlogFeed() produces an error var handleError = function(error) { alert(error); }; // Submit the request using the blogger service object bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Retrieve a list of blog posts


/* * Retrieve a list of blog posts */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0');

// The feed URI ued to retrieve a list of blogs for a particular logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method invoked when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); // Get list of posts for each blog for (var i = 0, blogEntry; blogEntry = blogEntries[i]; i++) { var postsFeedUri = blogEntry.getEntryPostLink().getHref(); var query = new google.gdata.blogger.BlogPostQuery(postsFeedUri); // Set the maximum number of blog posts to return query.setMaxResults(25); bloggerService.getBlogPostFeed(query, handleBlogPostFeed, handleError); } }; // A callback method invoked getBlogPostFeed() returns data var handleBlogPostFeed = function(postsFeedRoot) { var posts = postsFeedRoot.feed.getEntries(); // Display blog's title PRINT('Blog title: ' + postsFeedRoot.feed.getTitle().getText()); for (var i = 0, post; post = posts[i]; i++) { var postTitle = post.getTitle().getText(); var postURL = post.getHtmlLink().getHref(); PRINT('post: <b><a href="' + postURL + '" target="_blank">' + postTitle + '</a></b>'); } PRINT(''); }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Creating a new blog post


/* * Create a new blog post */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-

1.0'); // The feed URI ued to retrieve a list of blogs for a particular logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method invoked when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; // A callback invoked when getBlogPostFeed() returns the list of blog posts var handleBlogPostFeed = function(postsFeedRoot) { // Constuct an new BlogPostEntry var newEntry = new google.gdata.blogger.BlogPostEntry({ title: { type: 'text', text: 'JS-Blogger-Client: inserted post' }, content: { type: 'text', text: 'This is the body of the blog post. I can include <b>HTML</b> tags.' }, categories: [ {scheme: 'http://www.blogger.com/atom/ns#', term: 'Label1'}, {scheme: 'http://www.blogger.com/atom/ns#', term: 'Label2'} ] }); // mark post as a draft? // newEntry.setControl({draft: {value: google.gdata.Draft.VALUE_YES}}); postsFeedRoot.feed.insertEntry(newEntry, function() { PRINT('Blog post inserted:'); PRINT('"<b>' + newEntry.getTitle().getText() + '</b>"'); }, handleError ); }; var handleError = function(error) { alert(error); };

bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Retrieve a specific blog post


/* * Retrieve a specific blog post */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default "metafeed" feed is used to retrieve a list of blogs for a particular loggedin user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method that will be called when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; // Called when getBlogPostFeed() returns the list of blog posts var handleBlogPostFeed = function(postsFeedRoot) { var blogTitle = postsFeedRoot.feed.getTitle().getText(); var postEntry = postsFeedRoot.feed.getEntries()[0]; // only get first post var entryUri = postEntry.getSelfLink().getHref(); // post's uri // Get the blog post entry bloggerService.getBlogPostEntry(entryUri, function(postRoot) { var postTitle = postRoot.entry.getTitle().getText(); PRINT('Title of latest post to ' + blogTitle + ':'); PRINT('<b>"' + postTitle + '"</b>'); }, handleError ); }; var handleError = function(error) { alert(error); };

bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Update a blog post


/* * Update an existing blog post */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default "metafeed" feed is used to retrieve a list of blogs for a particular loggedin user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method that will be called when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; // Called when getBlogPostFeed() returns the list of blog posts var handleBlogPostFeed = function(postsFeedRoot) { var posts = postsFeedRoot.feed.getEntries(); // Use this string to identify the targeted blog post for the update var targetTitleStart = 'JS-Blogger-Client'; // Flag to indicate whether a match is found var postFound = false; for (var i = 0, postEntry; postEntry = posts[i]; i++) { var postTitle = postEntry.getTitle().getText(); // The first matched is located! if (postTitle.substring(targetTitleStart.length, 0) == targetTitleStart) { postFound = true; var entryUri = postEntry.getSelfLink().getHref(); break; } }

// If no matched post was found, print this message if (!postFound) { PRINT('Cannot find post title starting with: ' + targetTitleStart); } else { bloggerService.getBlogPostEntry(entryUri, handlePostEntry, handleError); } }; // This method is called when getBlogPostEntry() returns the blog post entry var handlePostEntry = function(postEntryRoot) { var postEntry = postEntryRoot.entry; // Update the blog's title postEntry.getTitle().setText('JS-Blogger-Client: updated title'); // Update the blog's content postEntry.setContent(google.gdata.Text.create('My updated post')); // Update a category var categories = postEntry.getCategories(); for(var i = 0, category; category = categories[i]; i++) { // Update a particular label if (category.getTerm() == 'Label1') { category.setTerm('Label1-updated'); break; } } // Update the blog post entry postEntry.updateEntry( function(updatedPostEntryRoot) { PRINT('Blog post updated.'); }, handleError ); }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Delete a blog post


/* * Delete an existing blog post */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-

1.0'); // The default feed used to retrieve a list of blogs for a logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method used when getBlogFeed() returns the list of our blogs var blogFeedCallback = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; var handleBlogPostFeed = function(blogPostsFeed) { var postEntries = blogPostsFeed.feed.getEntries(); // only get first post // Use this string to identify the targeted blog post for the update var targetTitleStart = 'JS-Blogger-Client'; // Flag to indicate whether a match is found var postFound = false; for (var i = 0, postEntry; postEntry = postEntries[i]; i++) { var postTitle = postEntry.getTitle().getText(); // The first matched is located! if (postTitle.substring(targetTitleStart.length, 0) == targetTitleStart) { postFound = true; var entryUri = postEntry.getSelfLink().getHref(); break; } } // If no matched post was found, print this message if (!postFound) { PRINT('Cannot find a post title starting with: ' + targetTitleStart); } else { postEntry.deleteEntry( function() { PRINT('Blog post deleted.'); }, handleError ); } }; // Error handler to be invoked when getBlogFeed() produces an error

var handleError = function(error) { alert(error); }; // Submit the request using the blogger service object bloggerService.getBlogFeed(feedUri, blogFeedCallback, handleError);

Query for blog posts


/* * Retrieve a specific blog post */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default feed used to retrieve a list of blogs for a logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method used when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); // Has the user created any blogs? if(!blogEntries.length) { PRINT('First <a href="http://www.blogger.com" target="_blank">create a blog</a>.'); return; } var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref(); // Define start/end published dates to restrict search to var startDate = new Date('May 31, 2008 14:00:00'); var endDate = new Date('June 1, 2008 15:42:00'); // Make query global to use in queryResultsCallback() query = new google.gdata.blogger.BlogPostQuery(postsFeedUri); query.setPublishedMin(new google.gdata.DateTime(startDate)); query.setPublishedMax(new google.gdata.DateTime(endDate)); // Pass in our query to getBlogPostFeed() instead of the entry's URI bloggerService.getBlogPostFeed(query, handleQueryResults, handleError); }; // A callback method invoked when getBlogPostFeed() returns the query results var handleQueryResults = function(resultsFeedRoot) {

var blogFeed = resultsFeedRoot.feed; var blogTitle = blogFeed.getTitle().getText(); // Retrieve our query parameters var startTime = google.gdata.DateTime.toIso8601(query.getPublishedMin()); var endTime = google.gdata.DateTime.toIso8601(query.getPublishedMax()); PRINT('Query: <b>' + blogTitle + '</b> posts between ' + startTime + ' and ' + endTime); var postEntries = blogFeed.getEntries(); for (var i = 0, posts; postEntry = postEntries[i]; i++) { var postTitle = postEntry.getTitle().getText(); var pubDate = google.gdata.DateTime.toIso8601(postEntry.getPublished().getValue()); PRINT('---'); PRINT('post title: <b>' + postTitle + '</b>'); PRINT('published: ' + pubDate); } }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Retrieve a list of comments


/* * Retrieve a list of blog post comments */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default feed used to retrieve a list of blogs for a logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method used when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref();

bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; var handleBlogPostFeed = function(blogPostsFeed) { var postEntry = blogPostsFeed.feed.getEntries()[0]; // only get first post var commentsFeedUri = postEntry.getRepliesLink().getHref(); bloggerService.getBlogCommentFeed(commentsFeedUri, handleCommentFeed, handleError); }; // The callback method used when getBlogCommentFeed() returns the list of our comments var handleCommentFeed = function(commentsFeedRoot) { var commentEntries = commentsFeedRoot.feed.getEntries(); if (commentEntries.length > 0) { for(var i = 0, commentEntry; commentEntry = commentEntries[i]; i++) { var commentTitle = commentEntry.getTitle().getText(); PRINT(commentTitle); } } else { PRINT('No comments found for that post'); } }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Create a comment
/* * Create a new blog post comment */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default feed used to retrieve a list of blogs for a logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method used when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) {

var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; // A callback invoked when getBlogPostFeed() returns the blog posts feed var handleBlogPostFeed = function(blogPostsFeedRoot) { var postEntries = blogPostsFeedRoot.feed.getEntries(); // Use this string to identify the targeted blog post to post a comment to var targetTitleStart = 'JS-Blogger-Client'; // Flag to indicate whether a match is found var postFound = false; for (var i = 0, postEntry; postEntry = postEntries[i]; i++) { var postTitle = postEntry.getTitle().getText(); // The first matched is located! if (postTitle.substring(targetTitleStart.length, 0) == targetTitleStart) { postFound = true; var postEntryId = postEntry.getId().getValue(); // Extract the blogID and postID from the entry's <id> tag var match = /blog-(\d+)\.post-(\d+)/.exec(postEntryId); // This is the uri for posting a new comment to this blog post var commentsFeedUri = 'http://www.blogger.com/feeds/' + match[1] + '/' + match[2] + '/comments/default'; break; } } // If no matched post was found, print this message if (!postFound) { PRINT('Cannot find a blog post title starting with: ' + targetTitleStart); } else { // Construct a new google.gdata.blogger.BlogCommentEntry var newComment = new google.gdata.blogger.BlogCommentEntry({ content: { type: 'text', text: 'JS-Blogger-Client: Great post!' } }); bloggerService.insertEntry(commentsFeedUri, newComment, insertCommentCallback, handleError);

} }; var insertCommentCallback = function(commentEntryRoot) { PRINT('Comment inserted.'); }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Delete a comment
/* * Delete a blog post comment */ // Create the blogger service object var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide1.0'); // The default feed used to retrieve a list of blogs for a logged-in user var feedUri = 'http://www.blogger.com/feeds/default/blogs'; // The callback method used when getBlogFeed() returns the list of our blogs var handleBlogFeed = function(blogFeedRoot) { var blogEntries = blogFeedRoot.feed.getEntries(); if (blogEntries.length) { var blogEntry = blogEntries[0]; // only get first blog // This is the feed uri for the blog's posts var postsFeedUri = blogEntry.getEntryPostLink().getHref(); bloggerService.getBlogPostFeed(postsFeedUri, handleBlogPostFeed, handleError); } }; // A callback invoked when getBlogPostFeed() returns the blog posts feed var handleBlogPostFeed = function(blogPostsFeedRoot) { var postEntries = blogPostsFeedRoot.feed.getEntries(); // Use this string to identify the targeted blog post to post a comment to var targetTitleStart = 'JS-Blogger-Client'; // Flag to indicate whether a match is found var postFound = false; for (var i = 0, postEntry; postEntry = postEntries[i]; i++) {

var postTitle = postEntry.getTitle().getText(); // The first matched is located! if (postTitle.substring(targetTitleStart.length, 0) == targetTitleStart) { postFound = true; // This is the uri for the blog post's commend feed var commentsFeedUri = postEntry.getRepliesLink().getHref(); break; } } // If no matched post was found, print this message if (!postFound) { PRINT('Cannot find a blog post title starting with: ' + targetTitleStart); } else { bloggerService.getBlogCommentFeed(commentsFeedUri, handleCommentsFeed, handleError); } }; var handleCommentsFeed = function(commentsFeedRoot) { var comments = commentsFeedRoot.feed.getEntries(); if (!comments.length) { PRINT('No comments left to delete.'); } else { // delete first comment comments[0].deleteEntry( function() { PRINT('Comment deleted.'); }, handleError); } }; var handleError = function(error) { alert(error); }; bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);

Vous aimerez peut-être aussi