I’ve seen too many questions in here (SO) asking about OAuth and how to connect to Facebook Graph API or Twitter API using OAuth protocol.
I’ve discovered JOAuth (from Google Code) and I was wondering how can I use it? What other features does JOAuth provide and does it fare well with other java oauth libraries?
Seeing that I’ve written JOAuth, I thought it would be appropriate to answer this question on SO. I didn’t find the option to make this question a community wiki. 🙁
Note I’m not here to discuss OAuth Authorization. There are various sites dedicated for this.
JOAuth comes with a wonderful feature. It has a controller
OAuthServletthat manages your HTTP Redirect response from the Service provider.The way to configure
OAuthServletto your web application, simply declare it as a<servlet>in yourweb.xmllike so:And your servlet mapping:
Now, that you have an OAuth servlet setup (bear in mind that
<load-on-startup>isn’t necessary but I like to have my servlets initialized before I use it), let’s talk about configuring JOAuth.The default JOAuth configuration file is
/WEB-INF/oauth-config.xml(hence it doesn’t have to be<init-param>in your servlet declaration).The configuration file looks as follows:
You’ll notice that each
<oauth>element has aversionattribute (it’s a compulsory attribute that’s needed by the controller to know which oauth flow to use). These only have 2 possible values (1for OAuth1 and2for OAuth 2).For OAuth 2, the
<consumer>element doesn’t have therequestTokenUrlattribute like its version 1 counterpart.The OAuth Service is the one responsible for the OAuth handling. Each
OAuthServiceis called by the controller through theexecute()method.There are 2 types of
OAuthService:com.neurologic.oauth.service.impl.OAuth1Service.com.neurologic.oauth.service.impl.OAuth2Service.Note For each service, if you’re using OAuth 2, you must have a service that extends
OAuth2Service. The same applies for OAuth 1. Failure to do that results in an exception being thrown.Each
<service>tag must have anameattribute that matches the<oauth>nameattribute (Case sensitive).Both
OAuth1ServiceandOAuth2Serviceexecute(HttpServletRequest, HttpServletResponse)have been implemented to best handle the flow of the OAuth authorization protocol, but you can override it if you’re not happy with it.An example of the
com.neurologic.music4point0.oauth.FacebookOAuthService:Since Facebook still uses OAuth 2 draft 0 (zero), their access token doesn’t do an HTTP 302 redirect, and that’s why
processReceivedAuthorization()is returns a null.The
processReceivedAuthorization()method allows the client to process received autorizationcodeand expects an authorization URL (that’s why it expects a return type ofString).If the method returns a
nullor an empty string, a url redirect never occurs.Once the oauth flow has completed, the path in the
<success>element is then called (through aRequestDispatcher), to show that OAuth is successfully completed.To access the Access Token, (after successful logon via OAuth), do the following:
I hope this little example helps those who are keen in making OAuth a worthwile experience for their development.
Sorry that I couldn’t find the
community wikicheckbox. Visit my blog (which has almost nothing on it) when you have time.Adieu 🙂
PS This is an implementation of the
TwitterOAuthService:Additional Resources