yet another blog about computer, technology, programming, and internet

Monday, May 02, 2011

Using ClientLogin to do Authentication for App Engine Application

Monday, May 02, 2011 Posted by Ismail Habib , 7 comments
General information about ClientLogin:

Discussion on Google Groups (including the original solution posted by geoffd123):

My solution uses Apache Http library instead of HttpUnit.
public static String loginToGoogle(String userid, String password,
   String appUrl) throws Exception {
  HttpClient client = new DefaultHttpClient();
  HttpPost post = new HttpPost(
  MultipartEntity reqEntity = new MultipartEntity();
  reqEntity.addPart("accountType", new StringBody("HOSTED_OR_GOOGLE",
    "text/plain", Charset.forName("UTF-8")));
  reqEntity.addPart("Email", new StringBody(userid));
  reqEntity.addPart("Passwd", new StringBody(password));
  reqEntity.addPart("service", new StringBody("ah"));
  reqEntity.addPart("source", new StringBody(
  HttpResponse response = client.execute(post);
  if (response.getStatusLine().getStatusCode() == 200) {
   InputStream input = response.getEntity().getContent();
   String result = IOUtils.toString(input);
   String authToken = getAuthToken(result);
   post = new HttpPost(appUrl + "/_ah/login?auth=" + authToken);
   response = client.execute(post);
   Header[] cookies = response.getHeaders("SET-COOKIE");
   for (Header cookie : cookies) {
    if (cookie.getValue().startsWith("ACSID=")) {
     return cookie.getValue();
   throw new Exception("ACSID cookie cannot be found");
  } else
   throw new Exception("Error obtaining ACSID");
A simple example on how to use it:

String authCookie = logonHelper.loginToGoogle("",
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("");
get.setHeader("Cookie", authCookie);
HttpResponse response = client.execute(get);


  1. thanks codenya bos, salam kenal

  2. Hi,
    this code is very helpful for me.. now i can easily do authentication.. thanks for sharing this information

  3. helpful blog, thanks for sharing.


  4. Thanks very much. I referenced this in

    1. glad that you found it useful!

  5. Oh guys you really have given the ever best code that’s really quite useful for me.Computer repair london

  6. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hopping for some more informative posts.