我尝试按照关于JOAuth 的说明进行操作,这是一个基于 java 的 OAuth 1(最终版)和 OAuth 2(草案 10)库。我该如何使用它?为了获取 facebook 访问令牌但没有成功。
我做了以下事情:
将这些行添加到 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:(将应用程序密钥和机密重命名为<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>
我的 com.facebook.FacebookOAuthService 类(OAuth 服务):
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<String, String> additionalParameters) throws OAuthException {
// TODO Auto-generated method stub
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) {
//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<String, String> additionalParameters) throws OAuthException {
// TODO Auto-generated method stub
}
}
最后是 start.jsp 文件,用户在成功时应该被转发到该文件。
<%@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 和 servlet 配置的新手,所以我可能错过了一些东西。
我错过了什么?
多谢!