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 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 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 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 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 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 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)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s