I tried to execute the instructions in JOAuth, the Java based OAuth 1 (final) and OAuth 2 (Draft 10) Java database. How to use it? to get facebook access token, but without success.
I have done the following:
added these lines to WEB-INF / web.xml
<servlet>
<description>An OAuth Servlet Controller</description>
<display-name>OAuthServlet</display-name>
<servlet-name>OAuthServlet</servlet-name>
<servlet-class>com.neurologic.oauth.servlet.OAuthServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/oauth-config.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>OAuthServlet</servlet-name>
<url-pattern>/oauth/*</url-pattern>
</servlet-mapping>
WEB-INF / oauth-config.xml was created with the following lines: (renamed application key and secret in <APP_KEY>and <APP_SECRET>)
<?xml version="1.0" encoding="UTF-8"?>
<oauth-config>
<oauth name="facebook" version="2">
<consumer key="<APP_KEY>" secret="<APP_SECRET>" />
<provider authorizationUrl="https://graph.facebook.com/oauth/authorize"
accessTokenUrl="https://graph.facebook.com/oauth/access_token" />
</oauth>
<service path="/oauth_redirect"
class="com.facebook.FacebookOAuthService" oauth="facebook">
<success path="/start.jsp" />
</service>
</oauth-config>
my com.facebook.FacebookOAuthService class (OAuth Service):
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;
public class FacebookOAuthService extends OAuth2Service {
private static final String REDIRECT_URL = "http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect";
@Override
protected String processReceivedAuthorization(HttpServletRequest request, String code, Map<String, String> additionalParameters) throws OAuthException {
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.setCode(code);
parameters.setRedirectUri(REDIRECT_URL);
Map<String, String> responseMap = getConsumer().requestAcessToken(GrantType.AUTHORIZATION_CODE, parameters, null, (String[])null);
if (responseMap == null) {
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;
}
@Override
protected void processAdditionalReceivedAccessTokenParameters(HttpServletRequest request, Map<String, String> additionalParameters) throws OAuthException {
}
}
and finally, the start.jsp file, which should be sent to the user with success.
<%@page import="com.neurologic.oauth.util.Globals"%>
<%
String accessToken = (String)request.getSession().getAttribute(Globals.SESSION_OAUTH2_ACCESS_TOKEN); //For OAuth 2 access token.
%>
<%= accessToken %>
, http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect, , , null, ,
, .
tomcat , - .
?
!