Páginas

SyntaxHighlighter

terça-feira, 8 de novembro de 2011

Using google-oauth-java-client to consume a Google App Engine OAuth protected resource

This post shows how to write a java based OAuth client to make requests against a Google App Engine OAuth protected resource using the google-oauth-java-client.

If you want to know how to create an OAuth provider or how to register your domain and get your consumer key and secret, I highly recommend you to read this great blog post written by Ikai Lan.
Actually, you should read it anyway, because the piece of code below just replaces the python script provided by him. Since I needed a java version I decided to write my own client.

The java class below contains a basic junit test which will do the 3-legged OAuth dance. The only thing I haven't implemented is the access token cache part - every time you run this test you will have to explicitly perform the authorization steps in the browser (again).

In  order to run it, you basically have to resolve 2 dependencies:
I used the 1.6.0-beta version. Just download and put them in the classpath and you are good to go!

And, of course, don't forget to change the APP_ID and CONSUMER_SECRET constants.

package com.ciandt.oauth.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.junit.Test;

import com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl;
import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthGetAccessToken;
import com.google.api.client.auth.oauth.OAuthGetTemporaryToken;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;

public class OAuthClientTest {

 private static final HttpTransport TRANSPORT = new NetHttpTransport();
 
 private static final String APP_ID = "your_app_id_here";
 private static final String CONSUMER_KEY = APP_ID + ".appspot.com";
 private static final String CONSUMER_SECRET = "your_consumer_secret_here";
 
 private static final String PROTECTED_SERVICE_URL = "https://" + APP_ID + ".appspot.com/resource";
 private static final String REQUEST_TOKEN_URL = "https://" + APP_ID + ".appspot.com/_ah/OAuthGetRequestToken";
 private static final String AUTHORIZE_URL = "https://" + APP_ID + ".appspot.com/_ah/OAuthAuthorizeToken";
 private static final String ACCESS_TOKEN_URL = "https://" + APP_ID + ".appspot.com/_ah/OAuthGetAccessToken";

 @Test
 public void consumeProtectedResource() throws Throwable {

  // this signer will be used to sign all the requests in the "oauth dance"
  OAuthHmacSigner signer = new OAuthHmacSigner();
  signer.clientSharedSecret = CONSUMER_SECRET;

  // Step 1: Get a request token. This is a temporary token that is used for 
  // having the user authorize an access token and to sign the request to obtain 
  // said access token.
  OAuthGetTemporaryToken requestToken = new OAuthGetTemporaryToken(REQUEST_TOKEN_URL);
  requestToken.consumerKey = CONSUMER_KEY;
  requestToken.transport = TRANSPORT;
  requestToken.signer = signer;

  OAuthCredentialsResponse requestTokenResponse = requestToken.execute();
  
  System.out.println("Request Token:");
  System.out.println("    - oauth_token        = " + requestTokenResponse.token);
  System.out.println("    - oauth_token_secret = " + requestTokenResponse.tokenSecret);

  // updates signer's token shared secret
  signer.tokenSharedSecret = requestTokenResponse.tokenSecret;

  OAuthAuthorizeTemporaryTokenUrl authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(AUTHORIZE_URL);
  authorizeUrl.temporaryToken = requestTokenResponse.token;
  
  // After the user has granted access to you, the consumer, the provider will
  // redirect you to whatever URL you have told them to redirect to. You can 
  // usually define this in the oauth_callback argument as well.
  String currentLine = "n";
  System.out.println("Go to the following link in your browser:\n"
    + authorizeUrl.build());
  InputStreamReader converter = new InputStreamReader(System.in);
  BufferedReader in = new BufferedReader(converter);
  while (currentLine.equalsIgnoreCase("n")) {
   System.out.println("Have you authorized me? (y/n)");
   currentLine = in.readLine();
  }
  
  // Step 3: Once the consumer has redirected the user back to the oauth_callback
  // URL you can request the access token the user has approved. You use the 
  // request token to sign this request. After this is done you throw away the
  // request token and use the access token returned. You should store this 
  // access token somewhere safe, like a database, for future use.
  OAuthGetAccessToken accessToken = new OAuthGetAccessToken(
    ACCESS_TOKEN_URL);
  accessToken.consumerKey = CONSUMER_KEY;
  accessToken.signer = signer;
  accessToken.transport = TRANSPORT;
  accessToken.temporaryToken = requestTokenResponse.token;

  OAuthCredentialsResponse accessTokenResponse = accessToken.execute();
  System.out.println("Access Token:");
  System.out.println("    - oauth_token        = " + accessTokenResponse.token);
  System.out.println("    - oauth_token_secret = " + accessTokenResponse.tokenSecret);
  System.out.println("\nYou may now access protected resources using the access tokens above.");

  // updates signer's token shared secret
  signer.tokenSharedSecret = accessTokenResponse.tokenSecret;

  OAuthParameters parameters = new OAuthParameters();
  parameters.consumerKey = CONSUMER_KEY;
  parameters.token = accessTokenResponse.token;
  parameters.signer = signer;

  // utilize accessToken to access protected resources
  HttpRequestFactory factory = TRANSPORT.createRequestFactory(parameters);
  GenericUrl url = new GenericUrl(PROTECTED_SERVICE_URL);
  HttpRequest req = factory.buildGetRequest(url);
  HttpResponse resp = req.execute();
  System.out.println("Response Status Code: " + resp.getStatusCode());
  System.out.println("Response body:" + resp.parseAsString());

 }

}

11 comentários:

  1. I was looking for this.. Thanks a lot. Appreciate your initiative.

    ResponderExcluir
  2. Excellent post. Very helpful. This is the first time I was able to use this to authenticate my Android client with my Google App Engine python app using OAuth. However, if I use a POST request instead of a GET requested for the protected resource URL, I can't authenticate. Anybody have any hints about where the problem might be?

    ResponderExcluir
  3. hi Fábio, i am a beginner in java web application programming, i am trying to write web application to use latitude for example a web page with few buttons and fields to show the current location and the history, i have cloned some example project for that and it workds and i get all the values in the eclipse console but that is a command line application but i have been unable to convert it into a eclipse web application with app engine.
    your code is really helpful but i dont know how to setup web application project in eclipse to execute your code to get to the authentication page or user consent as google calls it, once i get there i will be able to add the code for latitude then its no problem, i just need this procedure from creating a web application in eclipse to getting the access token, could you plz tell or share the project setup and how do you execute the program.
    Thanks

    ResponderExcluir
    Respostas
    1. Hi Faizan,

      Have a look at this example: http://code.google.com/p/google-plus-java-starter/
      There is even an App Engine webapp sample to checkout:

      http://code.google.com/p/google-plus-java-starter/source/browse/?r=61ffc8fb3021b6f94a7d9a4f641ee42d2d174e92#hg%2Fapp-engine%253Fstate%253Dclosed

      Hope that helps!

      Excluir
  4. hi Fábio
    what is App_id in the above example?
    is it the client id (like 887616088519.apps.googleusercontent.com)..plzz help

    ResponderExcluir
    Respostas
    1. Hi,

      APP_ID is the word you've chosen when you registered you app o GAE, which is also used to access it through HTTP.
      For example:

      http://myapp.appspot.com

      In this case the APP_ID is "myapp"

      Excluir
  5. I enhanced your code quite a bit, adding caching of the tokens as well as other goodies.

    https://github.com/jarrodhroberson/gae-oauth-client

    ResponderExcluir
  6. Why do we need to grand access, every time running your example? I thought that must only happen once?

    ResponderExcluir
    Respostas
    1. Because I'm not caching the tokens.
      Jarrod, created a more complete example with cache (see above and good luck!).

      Excluir
  7. Thank you very much for the code. I am new to Google OAuth,and OAuth in general and am trying this as a demo. After we include this file in the project (as mentioned in Ikai Lan's blog) how do we run the java file? If I deploy my app, there is no change in the output. How do we get the OAuth token and token secret values using the above code? I'm stuck, so please help me out.
    Thanks a lot in advance.

    ResponderExcluir