Java developer guide

Developer's Guide: Java

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 importstatements:
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 any of three approaches: OAuth authentication, AuthSub proxy authentication or ClientLoginusername/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.

OAuth authentication

For documentation about OAuth authentication using the Java GData library, please see OAuth in the Google Data Protocol Client Libraries.

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=http%3A%2F%2Fwww.blogger.com%2Ffeeds%2F&session=1&secure=0&next=http%3A%2F%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 = truewas 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:

Comments

Popular posts from this blog

Mysql data import