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 :
- JDK 1.6
- Eclipse 3.7 + Google Plugin for Eclipse
- 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>");
}
}
}
Both login or logout page are handle by GAE automatically, but the workflow is different :
- Run on local – It will simulate Google Accounts sign-in page (no password authentication).
- 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
Figure 2.2 : A simulated Google’s login screen, type something, no authentication.
Figure 2.3 : welcome, and display a logout link.
3. Deploy on GAE
Deploy Google App Engine, using application ID, “mkyong-java“.
Figure 3.1 – Access URL : http://mkyong-java.appspot.com/loginexample
Figure 3.2 – Redirect to actual Google account login screen.
Figure 3.3 – If Login is successful, redirect back to http://mkyong-java.appspot.com/loginexample
Download Source Code
Due to large file size, all GAE SDK dependency libraries are excluded.
It throws null pointer exception . If any have full code of above example please mail me [email protected]
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).
can u send the full code in my mail id [email protected]
I can run this code too. But problem is that how do I implement it with SpringMVC
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();
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..
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
How can we integrate User Service of GAE in Struts 2?
Would be really nice if you could write a tutorial on developing a spring mvc3 project on google-app-engine