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 , 35 comments
General information about ClientLogin: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html

Discussion on Google Groups (including the original solution posted by geoffd123):
http://groups.google.com/group/google-appengine-java/browse_thread/thread/c96d4fff73117e1d

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(
    "https://www.google.com/accounts/ClientLogin");
  
  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(
    "YourCompany-YourApp-YourVersion"));
  post.setEntity(reqEntity);
  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("email@gmail.com",
   "password","http://yourapp.appspot.com");  
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://yourapp.appspot.com/service");
get.setHeader("Cookie", authCookie);
HttpResponse response = client.execute(get);
response.getEntity().writeTo(System.out);

35 comments:

  1. thanks codenya bos, salam kenal

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

    ReplyDelete
  3. helpful blog, thanks for sharing.

    Michael

    ReplyDelete
  4. Thanks very much. I referenced this in http://stackoverflow.com/questions/9078003/accessing-oauth-protected-resource-on-google-app-engine/9405067#9405067

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

    ReplyDelete
  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.

    ReplyDelete
  7. Decent post. I was checking continually this web journal and I am awed! To a great degree accommodating data exceptionally the last part I tend to such information a considerable measure. I was looking for this specific data for quite a while. Much obliged to you and good fortunes. AppValley Download

    ReplyDelete
  8. Magnificent and intriguing article. Extraordinary things you've generally imparted to us. Much appreciated. Simply keep making this kind out of post. Emus4U Download

    ReplyDelete
  9. A debt of gratitude is in order for setting aside an ideal opportunity to talk about this, I feel firmly that adoration and read more on this theme. In the event that conceivable, for example, pick up learning, would you psyche upgrading your website with extra data? It is extremely valuable for me. NessTool APK

    ReplyDelete
  10. It would be ideal if you let me know in case you're searching for an article essayist for your site. You have some truly incredible posts and I feel I would be a decent resource. On the off chance that you ever need to take a portion of the heap off, I'd totally love to compose some material for your website in return for a connection back to mine. It would be ideal if you send me an email if intrigued. Much obliged to you! FlekStore Download

    ReplyDelete
  11. I am very interested in this post, everyone should know it more, wish you success. The author of this post is admirable, it brings a lot of emotion to everyone reading it, let's share it more widely.
    friv Games free 2019, Jogo para menina, playing friv games juegos, cá koi mini

    ReplyDelete