authentication against google in java

Google, Yahoo and others allows to authenticate users using OpenID protocol. Some pages are here and openid4j .

In my last post I have mentioned about google data libraries which is actually APIs for most of the google services, including to authenticate users and much more. GData libraries are let’s say higher level layer which handles all the openid pain for you. 2 simple lines of code and you are authenticate 🙂

Here is simple example

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.majecek.gdata</groupId>
 <artifactId>gdata_testing</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>gdata_testing</name>
 <url>http://maven.apache.org</url>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

<repositories>
<repository>
<id>mandubian-mvn</id>
<url>http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>com.google.gdata</groupId>
 <artifactId>gdata-calendar-2.0</artifactId>
 <version>1.41.3</version>
 </dependency>
 <dependency>
 <groupId>com.google.gdata</groupId>
 <artifactId>gdata-core-1.0</artifactId>
 <version>1.41.3</version>
 </dependency>
 </dependencies>
 </project>

Simple java project:

package com.majecek.gdata;
 import com.google.gdata.client.GoogleService;
 import com.google.gdata.client.calendar.CalendarService;
 import com.google.gdata.util.AuthenticationException;

public class App
 {
 public static void main( String[] args )
 {
 GoogleService myService = new CalendarService("some_apps");
 try {
 myService.setUserCredentials("userLogin".concat("@gmail.com"), "password");
 //if success then succes authentication occures
 System.out.println("success authentication");
 } catch (AuthenticationException e) {
 //if not then invalid credentials has been set
 //invalid username or password
 System.out.println("invalid authentication");
 }
 }
}

Example can be downloaded from google code repository. It is maven project.

I have updated and uploaded this example to github. Read README.

The bigest advantage is that we don’t need to handle any “openid” tricks, just creates service against google service (in my example Calendar) and call “setUserCredentials” which already authenticate user.

Credits go also @Petr Olivka

Advertisements

google data libraries in maven

I wanted to create new project and used google data libraries (com.google.gdata…) and connect with maven2.

Even though Google has these libraries as opensource, it is not in maven repository. Anyway there is project which added these google libraries into maven into google code.

Here is example of pom.xml (repository & libary dependency)

<project> ..

<repositories>
<repository>
 <id>mandubian-mvn</id>
 <url>http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository</url>
 </repository>
</repositories>
.......
<dependencies>
 <dependency>
  <groupId>com.google.gdata</groupId>
  <artifactId>gdata-calendar-2.0</artifactId>
  <version>1.41.3</version>
 </dependency>
 <dependency>
  <groupId>com.google.gdata</groupId>
  <artifactId>gdata-core-1.0</artifactId>
  <version>1.41.3</version>
  </dependency>
</dependencies>
....
</project>

As you can see you can add google libraries according your needs. If you don’t know the full name and version of the library you want to use – check it here .


GWT using maven2

I was trying to set up gwt with maven2. And here is tutorials how to do it.

We will use little help. There is already gwt archetype for maven2. (you don’t have to use it. You are fine with maven default war project archetype but set up your project correctly)

  1. Download this file (it is for version 1.4.59, but by changing the pom you can get the newest version (which is at the time of writing 1.4.61)
  2. unzip file
  3. install it to your repository “mvn install”

Use the archetype to create maven gwt skelet.

mvn archetype:create -DarchetypeGroupId=com.totsp.gwt -DarchetypeArtifactId=gwt-archetype -DarchetypeVersion=1.0-SNAPSHOT -DartifactId=gwt-using-archetype -DgroupId=foo.bar

Next we will modify pom.xml

  • change version for gwt-user and gwt-servlet (at the time of writing it is 1.4.61)
  • change element <compileTarget> to <compileTargets> also for closing element </compileTargets>
  • last thing is to tell where is your gwt installation. So add <gwtHome> element (here is example:
<build>
......
  <plugin>
   <groupId>com.totsp.gwt</groupId>
   <artifactId>maven-googlewebtoolkit2-plugin</artifactId>
   <configuration>
    <gwtHome>C:/programs/gwt-windows-1.4.61</gwtHome>

This is very “hardcoded” and it is suggest to define it in ~/.m2/settings.xml (use either way) and here is example of settings.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
 <settings>
  <profiles>
   <profile>
    <id>gwt-1.4.61</id>
    <properties>
     <google.webtoolkit.home>C:/programs/gwt-windows-1.4.61</google.webtoolkit.home>
     <!–- XstartOnFirstThread needed only on the mac -–>
     <!–-<google.webtoolkit.extrajvmargs>-XstartOnFirstThread</google.webtoolkit.extrajvmargs>–->
    </properties>
   </profile>
  </profiles>
  <activeProfiles>
   <activeProfile>gwt-1.4.61</activeProfile>
  </activeProfiles>
 </settings>

OK and now we are ready to try our example. Run ‘mvn gwt:gwt’ and it will launch browser with your application. By using archetype you are up and running in few minutes.

If you want to integrate it with jetty just run ‘mvn jetty:run-exploded’ and open localhost:8080.

Few tips:

  • polish tutorial with little bit different approach
  • tutorial I studied from
  • Multi project in maven with GWT – use svn to check out code on your local computer and run it

Source code can be downloaded from here.