Important: This is an old version of this page. For the latest version, use the links in the left-side navbar.
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 Java 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
Audience
This document is intended for programmers who want to write Java client applications that can interact with Blogger.
This document assumes that you understand the general ideas behind the Google Data APIs protocol.
For reference information about the classes and methods provided by the client library, see the Java client library API reference. For general Blogger Data API reference information, see the Protocol reference guide.
Getting started
For help setting up the client library, see the Getting Started Guide.
The Java client library requires Java 1.5. After downloading
the client library, you'll find the classes you need to get started in the
java/lib/gdataclient-1.0.jar
file.
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.
Running the sample code
A full working sample client, containing all the sample code shown in this
document, is available in the Java client library distribution, under the
directory gdata/java/sample/blogger/BloggerClient.java
. Build and
execution instructions are included in the same directory in the
README.txt
file.
The sample client performs several operations on the provided blog to demonstrate the use of the Blogger Data API.
To compile the examples in this document into your own code, you'll need the
following import
statements:
import com.google.gdata.client.*; import com.google.gdata.data.*; import com.google.gdata.util.*; import java.io.IOException; import java.net.URL;
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. It can authenticate using either of two approaches: AuthSub proxy authentication or ClientLogin username/password authentication.
For more information about authentication with Google Data APIs in general, see the authentication documentation.
Most of the samples in subsequent sections of this document assume you have
an authenticated GoogleService
object.
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. For more detailed information, see the AuthSub documentation.
When a user first visits your application, they have not yet been authenticated. In this case, you need to display some information and a link directing the user to a Google page to authenticate your request for access to their blogs. The Java client library provides a function to generate the Google page's URL. The code below retrieves the URL of the AuthSubRequest page:
String next = "http://www.example.com/welcome.html"; String scope = "http://www.blogger.com/feeds/"; boolean secure = false; boolean session = true; String authSubLogin = AuthSubUtil.getRequestUrl(next, scope, secure, session);
The getRequestUrl
method takes the following parameters
(corresponding to the query parameters used by the AuthSubRequest handler):
- next
- The URL of the page that Google should redirect the user to after authentication.
- scope
- Indicates that the application is requesting a token to access Blogger feeds. The scope string to use is
http://www.blogger.com/feeds/
(URL-encoded, of course). - secure
- Indicates whether the client is requesting a secure token.
- session
- Indicates whether the token returned can be exchanged for a multi-use (session) token.
The above example shows a call that doesn't request a secure token (the value of secure
is false
). The resulting request URL might look like this:
https://www.google.com/accounts/AuthSubRequest?scope=https%3A%2F%2F1.800.gay%3A443%2Fhttp%2Fwww.blogger.com%2Ffeeds%2F&session=1&secure=0&next=https%3A%2F%2F1.800.gay%3A443%2Fhttp%2Fwww.example.com%2Fwelcome.html
The user follows the link to Google's site and authenticates to their Google Account.
After the user authenticates, the AuthSub system redirects them to the URL
you specified in the next
query parameter of the AuthSubRequest
URL. The AuthSub system appends an authentication token to that URL, as the
value of the token
query parameter. For example:
http://www.example.com/welcome.html?token=yourAuthToken
This token value represents a single-use AuthSub token. In this example,
since session = true
was specified, this token can be exchanged for
an AuthSub session token by calling the AuthSubSessionToken
service, as follows, where urlFromAuthSub
is the URL that AuthSub
appended the token to:
String token = AuthSubUtil.getTokenFromReply(urlFromAuthSub); String sessionToken = AuthSubUtil.exchangeForSessionToken(token, null);
That is, you pass your one-time-use token to the
exchangeForSessionToken
method, along with either null
(for unregistered mode) or a private key (for registered mode), and the AuthSub
interface returns a session token. For more information about registered
applications and private keys, see the "Signing requests"
section of the AuthSub documentation.
Your application can then use the session token in subsequent interactions
with Blogger. To tell the Java client library to automatically send the session
token with each request, call the GoogleService
object's
setAuthSubToken
method:
GoogleService.setAuthSubToken(sessionToken, null);
After that, the client library automatically sends the token along with every request.
ClientLogin username/password authentication
Use ClientLogin authentication if your client is a standalone, single-user
"installed" client (such as a desktop application). Just call the
setUserCredentials
method on your GoogleService
object
and all subsequent interactions with Blogger will be authenticated:
GoogleService myService = new GoogleService("blogger", "exampleCo-exampleApp-1"); myService.setUserCredentials("[email protected]", "secretPassword");
In the snippet above, we pass two parameters to the
GoogleService
constructor. The first parameter is the name of the
service we want to interact with. The second parameter is the name of our
application in the form
companyName-applicationName-versionID.
For more information about ClientLogin authentication, including sample requests and responses, see the Authentication for Installed Applications documentation.
Note: Use the same token for all requests in a given session; don't acquire a new token for each Blogger request.
Note: As described in the ClientLogin
documentation, the authentication request may fail and request a CAPTCHA
challenge. If you want Google to issue and handle the CAPTCHA challenge, then
send the user to
https://www.google.com/accounts/DisplayUnlockCaptcha?service=blogger
(rather than to the CAPTCHA-handling URL given in the ClientLogin
documentation).
Retrieving a list of blogs
The Blogger Data API provides a feed that lists the blogs for a particular user; that feed is known as a "metafeed."
The following sample code uses an authenticated GoogleService
object to retrieve the metafeed and then prints each blog's title.
public static void printUserBlogs(GoogleService myService) throws ServiceException, IOException { // Request the feed final URL feedUrl = new URL("http://www.blogger.com/feeds/default/blogs"); Feed resultFeed = myService.getFeed(feedUrl, Feed.class); // Print the results System.out.println(resultFeed.getTitle().getPlainText()); for (int i = 0; i < resultFeed.getEntries().size(); i++) { Entry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } }
Note the URL used by the getFeed
method. This is the default
metafeed URL; it returns a list of blogs for the currently authenticated user.
To access a feed for a different user, you can put the user's ID in place of
default
in the metafeed URL. The user's ID is the string of digits
at the end of the user's profile URL.
Creating posts
The Blogger Data API allows you to create and publish new blog entries, as well as creating drafts of entries.
Note: Setting a custom author for posts is currently not supported. All new posts will appear as if they were created by the currently authenticated user.
Publishing a blog post
You can use the Java client library to publish new blog entries.
First, create an Entry
object to represent the blog post. Then
you can set the title, content and other attributes of the blog post. Finally,
use the GoogleService
object to insert the post. Here's an example
of how to publish a new blog post:
public static Entry createPost( GoogleService myService, String blogID, String title, String content, String userName) throws ServiceException, IOException { // Create the entry to insert Entry myEntry = new Entry(); myEntry.setTitle(new PlainTextConstruct(title)); myEntry.setContent(new PlainTextConstruct(content)); // Ask the service to insert the new entry URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default"); return myService.insert(postUrl, myEntry); }
The insert
method takes the service's post URL as a parameter.
Then the method returns the entry as it was stored by Blogger. The entry
returned is the same one you sent, but it also contains various elements added
by Blogger, such as a post ID.
If your request fails for some reason, Blogger may return a different status code. For information about the status codes, see the Google Data API protocol reference document.
Creating a draft blog post
Draft posts are created in the same way as public posts, but you need to set
the draft
attribute of the Entry
object. You can
create a blog post like the one above as a draft by adding the highlighted
line:
public static Entry createPost(GoogleService myService, String blogId, String title, String content, String userName, Boolean isDraft) throws ServiceException, IOException { // Create the entry to insert Entry myEntry = new Entry(); myEntry.setTitle(new PlainTextConstruct(title)); myEntry.setContent(new PlainTextConstruct(content)); myEntry.setDraft(isDraft); // Ask the service to insert the new entry URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default"); return myService.insert(postUrl, myEntry); }
You can turn an existing draft blog post into a published post by retrieving the draft post, setting the draft attribute to false, and then updating the post. We'll cover retrieving and updating posts in the next two sections.
Retrieving posts
The following sections describe how to retrieve a list of blog posts, with and without query parameters.
You can query a Blogger public feed without authentication. Therefore, you
don't need to call the setUserCredentials
method or do AuthSub
authentication before retrieving posts from a public blog.
Retrieving all blog posts
To retrieve the user's posts, call the same getFeed
method used
to retrieve the blogs metafeed, but this time send the blog-post feed URL:
public static void printAllPosts( GoogleService myService, String blogId) throws ServiceException, IOException { // Request the feed URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default"); Feed resultFeed = myService.getFeed(feedUrl, Feed.class); // Print the results System.out.println(resultFeed.getTitle().getPlainText()); for (int i = 0; i < resultFeed.getEntries().size(); i++) { Entry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); } System.out.println(); }
Retrieving posts using query parameters
The Blogger Data API lets you request a set of entries that match specified
criteria, such as requesting blog posts published or updated in a given date
range. To do this, you create a Query
object and pass it to the
GoogleService.getQuery
method.
For example, to send a date-range query, use the setPublishedMin
and setPublishedMax
methods of the Query
object. The
following code snippet prints the title of each blog post published between the
given start time and end time:
public static void printDateRangeQueryResults( GoogleService myService, String blogId, DateTime startTime, DateTime endTime) throws ServiceException, IOException { // Create query and submit a request URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default"); Query myQuery = new Query(feedUrl); myQuery.setPublishedMin(startTime); myQuery.setPublishedMax(endTime); Feed resultFeed = myService.query(myQuery, Feed.class); // Print the results System.out.println(resultFeed.getTitle().getPlainText() + " posts between " + startTime + " and " + endTime); for (int i = 0; i < resultFeed.getEntries().size(); i++) { Entry entry = resultFeed.getEntries().get(i); System.out.println("\t" + entry.getTitle().getPlainText()); System.out.println("\t" + entry.getUpdated().toStringRfc822()); } System.out.println(); }
Notice that the Query
object is constructed using the same post feed URL used to retrieve posts.
The Blogger Data API supports the following Query
methods:
- addCategoryFilter
- Specify categories (also known as labels) to filter the feed results. For example,
http://www.blogger.com/feeds/blogID/posts/default/-/Fritz/Laurie
returns entries with both the labelsFritz
andLaurie
. - setMaxResults
- Set the maximum number of entries to return.
- setPublishedMin, setPublishedMax
- Set the bounds on entry publication dates.
- setStartIndex
- Set the 1-based index of the first result to be retrieved (for paging).
- setUpdatedMin, setUpdatedMax
- Set the bounds on entry update dates. These query parameters are ignored unless the
orderby
parameter is set toupdated
.
Note: There are currently no setters for the
orderby
query parameter. However, you can still use the
Query.addCustomParameter()
method if you need to set this.
For more information about query parameters, see the Blogger Data API Reference Guide and the Google Data APIs Reference Guide.
Updating posts
To update an existing blog post, first you retrieve the entry you want to
update, then you modify it, and then you send it to Blogger using the
update
method. The following code snippet modifies the title of a
blog entry, assuming that you've already retrieved the entry from the
server.
public static Entry updatePostTitle( GoogleService myService, Entry entryToUpdate, String newTitle) throws ServiceException, IOException { entryToUpdate.setTitle(new PlainTextConstruct(newTitle)); URL editUrl = new URL(entryToUpdate.getEditLink().getHref()); return myService.update(editUrl, entryToUpdate); }
The above code returns an Entry
containing the entire
newly-updated post. To update any other properties, simply set them in the
Entry
object before calling update
.
Note: Modifying the author data associated with posts is currently not supported.
Deleting posts
To delete a post, pass the post's edit URL to the delete
method
on your GoogleService
object, like this:
public static void deletePost( GoogleService myService, String editLinkHref) throws ServiceException, IOException { URL deleteUrl = new URL(editLinkHref); myService.delete(deleteUrl); }
Comments
The Blogger Data API allows for creating, retrieving, and deleting comments. Updating comments is not supported (nor is it available in the web interface).
Creating comments
To post a comment, create an Entry
object and insert it as
follows:
public static Entry createComment( GoogleService myService, String blogID, String postId, String commentText) throws ServiceException, IOException { // Build the comment feed URI String commentsFeedUri = "http://www.blogger.com/feeds/" + blogID + "/" + postId + "/comments/default"; URL feedUrl = new URL(commentsFeedUri); // Create a new entry for the comment and submit it to the GoogleService Entry myEntry = new Entry(); myEntry.setContent(new PlainTextConstruct(commentText)); return myService.insert(feedUrl, myEntry); }
Note: Currently, you can only post comments to a blog owned by the authenticated user.
Note: Setting a custom author for comments is currently not supported. All new comments will appear as if they were created by the currently authenticated user.
Retrieving comments
You can retrieve the comments for a particular post from the post's comments feed URL:
public static void printAllComments( GoogleService myService, String blogID, String postId) throws ServiceException, IOException { // Build comment feed URI and request comments on the specified post String commentsFeedUri = "http://www.blogger.com/feeds/" + blogID + "/" + postId + "/comments/default"; URL feedUrl = new URL(commentsFeedUri); Feed resultFeed = myService.getFeed(feedUrl, Feed.class); // Display the results System.out.println(resultFeed.getTitle().getPlainText()); for (int i = 0; i < resultFeed.getEntries().size(); i++) { Entry entry = resultFeed.getEntries().get(i); System.out.println("\t" + ((TextContent) entry.getContent()).getContent().getPlainText()); System.out.println("\t" + entry.getUpdated().toStringRfc822()); } System.out.println(); }
Or you can get the comments from all posts by using the blog's comments feed URL:
http://www.blogger.com/feeds/blogID/comments/default
Deleting comments
To delete a comment, pass the comment's edit URL to the delete
method on your GoogleService
object like this:
public static void deleteComment(GoogleService myService, String editLinkHref) throws ServiceException, IOException { URL deleteUrl = new URL(editLinkHref); myService.delete(deleteUrl); }