Friday, December 22, 2006

Google Apps for Your Domain API in Java

Implementation of Google Apps for Your Domain Provisioning API in Java.

You can see a live demo at my java hosting

Please note that eatj automatically stops the server after 6 hours so above link might not work. The war file includes the extra libraries required for the sample to work. (commons codec, commons httpclient, commons logging)

Download the source code (war)

Servlet which takes the request from the form to perform lookup to google webserver

package com.uttarainfo.googleapp;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;


* Servlet implementation class for Servlet: GoogleHelper



public class GoogleHelper extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#HttpServlet()


public GoogleHelper() {



/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

String token = (new GoogleAppToken()).getToken(request.getParameter("Email"), request.getParameter("Passwd"));

PrintWriter out = response.getWriter();

if (token!=null){

HttpSession session = request.getSession();

session.setAttribute("token", token);

out.print("successful login");

} else {

out.print("login failed. Go back to the previous page and try again");




Below is helper class that actually gets the Token from google server

package com.uttarainfo.googleapp;


import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.PostMethod;

public class GoogleAppToken {

private static String url = "";

public String getToken(String email, String password) {

try {

HttpClient client = new HttpClient();

PostMethod method = new PostMethod(url);

method.addParameter("accountType", "HOSTED");

method.addParameter("Email", email);

method.addParameter("Passwd", password);

int statusCode = client.executeMethod(method);


if (statusCode == 200) {


String contents = method.getResponseBodyAsString();


String[] response = contents.split("\n");

if (response != null) {


//The first line contains the SID, split that line by the "="

String[] SID = response[0].split("=");

if (SID != null) {


return SID[1];

} else

System.out.println("SID was not included within the content within the authentication request.");

} else

System.out.println("No content was returned from Google during the authentication request.");

} else

System.out.println("Error posting authentication request to Google. Status code returned: "

+ statusCode);

} catch (HttpException e) {

// TODO Auto-generated catch block


} catch (IOException e) {

// TODO Auto-generated catch block



return null;



Download the source code (war) You can just place the war file in tomcat and try it out for yourself

powered by performancing firefox

Technorati Tags: , ,

1 comment:

  1. Where is the source code? The download is a war file, not java inside.