Thursday, January 24, 2013

Simple Google-OAuth2 from a Java client

OAuth is a powerful way to ensure that clients have access to appropriate resources, and the Google OAuth libs do a lot of the heavy lifting for you. But while the doco is pretty good I found it lacking when looking for the most appropriate way to authorize for a simple Java (non-web) application and that's probably to do with the rate at which this area is moving.

It turns out that there are excellent classes in the Google libraries to make OAuth absolutely trivial, in fact it's really only 3 lines:
  1. Construct your AuthorizationCodeFlow
  2. Construct your AuthorizationCodeInstalledApp
  3. Ask the InstalledApp to authorize the client
Here's the full code listing:


Now to make this happen you're going to need to include the relevant Google libs. You can find lots of good info about the libs here google-api-java-client, google-oauth-java-client and google-http-java-client.

The libs necessary for the above are:

2 comments:

  1. Great !!

    Please note:

    1) maven repo


    google-api-services
    http://mavenrepo.google-api-java-client.googlecode.com/hg

    true


    true




    2) And current drive API

    com.google.apis
    google-api-services-drive
    v2-rev9-1.8.0-beta


    Thanks.

    ReplyDelete
  2. Thanks Olivier, I've added those changes.

    I was using http://google-api-client-libraries.appspot.com/mavenrepo as the repository. Which I seem to have taken from http://code.google.com/p/google-api-java-client/wiki/APIs.

    But perhaps the one I was using is an internal Google maven repository because it can't be navigated by browser and it contains more recent artifacts such as google-api-services-drive:v2-rev41-1.13.2-beta

    ReplyDelete

Note: Only a member of this blog may post a comment.