Wednesday, 12 December 2012

Google Plus authentication Using OAuth 2.0

This script
- uses OAuth 2.0 in Java servlets to authenticate users on your website using Google Plus.
- pulls the user details from its G+ account and displays the same on your webpage.
- has logout facility.

Download Pack     Live Demo    

How to use:

The download pack contains the complete project structure with the sample war file that I have deployed in my live demo.

1. Register your domain with google

Click here to navigate to the google registration page and add your new domain.

Then click on the link "Manage []" under Manage registration section.

Verify your domain by uploading an html file that google provides.

Now fill in the details and hit Save. Uploading the certificate is optional.

2. Enable Google Plus API service

Navigate to the API console. In the services section, enable Google+ API service.

In the "API Access" section, click the button that says "Create OAuth2.0 Client ID".

Fill in the branding details and click next.

On the Client ID setting page click the more options link and specify the callback URL. This is a servlet in our case that will receive the code/token upon authentication.

Now you have a Google+ ClientID and Client secret for your web application.

3. Web.xml (Input variables)

Specify these Client ID and Client secret in your web.xml
Also the additional permissions are mentioned here. You may update these permissions as required.
After successful login welcome.jsp will be shown to the user.

3. Welcome.jsp (Output variables)

The user account details will be available as session attributes in welcome.jsp
The object googlePlusUser contains these attributes as shown. You may write your own display code instead, as required.



  1. type Exception report


    description The server encountered an internal error () that prevented it from fulfilling this request.


    javax.servlet.ServletException: java.lang.NullPointerException

    root cause


    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.

  2. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.