Main Tutorials

GAE + Java – Integrating Google user account

For this tutorial, we will show you how to integrate Google user account in GAE + Java project, via Google Java SDK UserService class.

Tools used :

  1. JDK 1.6
  2. Eclipse 3.7 + Google Plugin for Eclipse
  3. Google App Engine Java SDK 1.6.3.1

1. GAE User service example

If user login by using their Google account, display a welcome message and a “Logout” link; Otherwise display a “Login” link.


package com.mkyong.user;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginExampleServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		resp.setContentType("text/html");
		resp.getWriter().println("<h2>GAE - Integrating Google user account</h2>");
		
		if (user != null) {
			
			resp.getWriter().println("Welcome, " + user.getNickname());
			resp.getWriter().println(
				"<a href='"
					+ userService.createLogoutURL(req.getRequestURI())
					+ "'> LogOut </a>");

		} else {

			resp.getWriter().println(
				"Please <a href='"
					+ userService.createLoginURL(req.getRequestURI())
					+ "'> LogIn </a>");

		}
	}
}
Note
Both login or logout page are handle by GAE automatically, but the workflow is different :

  1. Run on local – It will simulate Google Accounts sign-in page (no password authentication).
  2. Run on GAE – It will redirect to actual Google Account login screen.

2. Run it locally

Right click on the project and run as “Web Application”. By default, it run at post 8888.

Figure 2.1 : Access URL : http://localhost:8888/loginexample

GAE integrating google account and run it local

Figure 2.2 : A simulated Google’s login screen, type something, no authentication.

GAE integrating google account and run it local

Figure 2.3 : welcome, and display a logout link.

GAE integrating google account and run it local

3. Deploy on GAE

Deploy Google App Engine, using application ID, “mkyong-java“.

Figure 3.1 – Access URL : http://mkyong-java.appspot.com/loginexample

GAE integrating google account and run it on GAE

Figure 3.2 – Redirect to actual Google account login screen.

GAE integrating google account and run it on GAE

Figure 3.3 – If Login is successful, redirect back to http://mkyong-java.appspot.com/loginexample

GAE integrating google account and run it on GAE

Download Source Code

Due to large file size, all GAE SDK dependency libraries are excluded.

Download it – GAE-UserService-LoginExample.zip (8 KB)

References

  1. Using the Users Service
  2. The Users Java API
  3. GAE UserService JavaDoc
  4. GAE Java hello world example

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
K Ravi
4 years ago

It throws null pointer exception . If any have full code of above example please mail me [email protected]

Svante Salen
8 years ago

For this to work; you need to create a Google “Web Application Project” using the google plugin mentioned above.
It would be nice to know how to just add the google login to an already present project (that’s my current situation).

Sweety
8 years ago

can u send the full code in my mail id [email protected]

peng li
9 years ago

I can run this code too. But problem is that how do I implement it with SpringMVC

Rajavardhan Merugu
9 years ago

Hii
When am running the program, am getting NullPointerException. In the below line.
Will please tell me How to get the UserSerivce Object.
UserService userService = UserServiceFactory.getUserService();

vivek mehta
10 years ago

I want the full Example code In Java can u please send me the full code on my email :[email protected].
It’s emergency for me.

Thanx in Advance..

Sai
11 years ago

Logout scenario is not working.
Everytime, i need to open my mail id and click logout , to make login work for my Login example URL.

COuld you please help to resolve logout issue

JR Galia
11 years ago

How can we integrate User Service of GAE in Struts 2?

sk
12 years ago

Would be really nice if you could write a tutorial on developing a spring mvc3 project on google-app-engine