title: java tomcat: how to use JOAuth properly
body: <p>I tried to follow the instructions on <a href="https://stackoverflow.com/questions/4376612/joauth-a-java-based-oauth-1-final-and-oauth-2-draft-10-library-how-do-i-use">JOAuth, a java-based OAuth 1 (final) and OAuth 2 (draft 10) library. How do I use it?</a> in order to fetch facebook access token but with no success.</p>
  
  <p>i did the following:</p>
  
  <p>added these lines to WEB-INF/web.xml</p>
  
  <pre><code>&lt;servlet&gt;
    &lt;description&gt;An OAuth Servlet Controller&lt;/description&gt;
    &lt;display-name&gt;OAuthServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;OAuthServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.neurologic.oauth.servlet.OAuthServlet&lt;/servlet-class&gt;
    &lt;init-param&gt;
       &lt;param-name&gt;config&lt;/param-name&gt;
       &lt;param-value&gt;/WEB-INF/oauth-config.xml&lt;/param-value&gt;
    &lt;/init-param&gt;
    &lt;load-on-startup&gt;3&lt;/load-on-startup&gt;
   &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
   &lt;servlet-name&gt;OAuthServlet&lt;/servlet-name&gt;
   &lt;url-pattern&gt;/oauth/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>created WEB-INF/oauth-config.xml with the following lines:
  (renamed app key and secret to <code>&lt;APP_KEY&gt;</code> and <code>&lt;APP_SECRET&gt;</code>)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;oauth-config&gt;
  &lt;oauth name="facebook" version="2"&gt;
   &lt;consumer key="&lt;APP_KEY&gt;" secret="&lt;APP_SECRET&gt;" /&gt;
   &lt;provider authorizationUrl="https://graph.facebook.com/oauth/authorize" 
     accessTokenUrl="https://graph.facebook.com/oauth/access_token" /&gt;
  &lt;/oauth&gt;
  
  &lt;service path="/oauth_redirect" 
  class="com.facebook.FacebookOAuthService" oauth="facebook"&gt;
   &lt;success path="/start.jsp" /&gt;
  &lt;/service&gt;
  &lt;/oauth-config&gt;
  </code></pre>
  
  <p>my com.facebook.FacebookOAuthService class ( The OAuth Service ):</p>
  
  <pre><code>package com.xpogames.facebook;
  
  import java.util.Map;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import net.oauth.enums.GrantType;
  import net.oauth.exception.OAuthException;
  import net.oauth.parameters.OAuth2Parameters;
  
  import com.neurologic.oauth.service.impl.OAuth2Service;
  import com.neurologic.oauth.util.Globals;
  
  /**
   * @author The Elite Gentleman
   * @since 05 December 2010
   *
   */
  public class FacebookOAuthService extends OAuth2Service {
  
  
   private static final String REDIRECT_URL = "http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect";
  
       /* (non-Javadoc)
        * @see com.neurologic.oauth.service.impl.OAuth2Service#processReceivedAuthorization(javax.servlet.    http.HttpServletRequest, java.lang.String, java.util.Map)
    */
    @Override
    protected String processReceivedAuthorization(HttpServletRequest request, String code, Map&lt;String, String&gt; additionalParameters) throws OAuthException {
   // TODO Auto-generated method stub
  
    OAuth2Parameters parameters = new OAuth2Parameters();
    parameters.setCode(code);
    parameters.setRedirectUri(REDIRECT_URL);
  
    Map&lt;String, String&gt; responseMap = getConsumer().requestAcessToken(GrantType.AUTHORIZATION_CODE, parameters, null, (String[])null);
    if (responseMap == null) {
     //This usually should never been thrown, but we just do anyway....
     throw new OAuthException("No OAuth response retrieved.");
    }
  
    if (responseMap.containsKey("error")) {
     throwOAuthErrorException(responseMap);
    }
  
    if (responseMap.containsKey(OAuth2Parameters.ACCESS_TOKEN)) {
     String accessToken = responseMap.remove(OAuth2Parameters.ACCESS_TOKEN);
     request.getSession().setAttribute(Globals.SESSION_OAUTH2_ACCESS_TOKEN, accessToken);
     processAdditionalReceivedAccessTokenParameters(request, responseMap);
    }
  
    return null;
   }
  
   /* (non-Javadoc)
    * @see com.neurologic.oauth.service.impl.OAuth2Service#processAdditionalReceivedAccessTokenParamet    ers(javax.servlet.http.HttpServletRequest, java.util.Map)
    */
   @Override
   protected void processAdditionalReceivedAccessTokenParameters(HttpServletRequest request, Map&lt;String, String&gt; additionalParameters) throws OAuthException {
    // TODO Auto-generated method stub
  
   }   
  }
  </code></pre>
  
  <p>and finally the start.jsp file that the user should be forwarded to on success.</p>
  
  <pre><code>&lt;%@page import="com.neurologic.oauth.util.Globals"%&gt;
  
  &lt;% 
  String accessToken =     (String)request.getSession().getAttribute(Globals.SESSION_OAUTH2_ACCESS_TOKEN); //For OAuth 2 access token.
  %&gt;
  &lt;%= accessToken %&gt;
  </code></pre>
  
  <p>when I try to test it by forwarding my browser to <a href="http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect" rel="nofollow noreferrer">http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect</a> the output that i get is <code>null</code> which means that the attribute does not exist</p>
  
  <p>there are no errors but still i get no proper token.</p>
  
  <p>I'm new to tomcat and the servlet configuration so i might have missed something. </p>
  
  <p>what am i missing?</p>
  
  <p>thanks a lot!</p>