我尝试按照JOAuth, a java-based OAuth 1 (final) and OAuth 2 (draft 10) library. How do I use it?上的说明获取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>
和<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配置的新手,所以我可能错过了一些东西。
我错过了什么?
非常感谢!
答案 0 :(得分:4)
好的,我以前从未回答的问题(因为我假定用户知道OAuth授权)是OAuth授权流程的启动。
首先,按照规定here遵循OAuth流程。我正在向你展示如何根据记录的内容在java中完成它。
要使此工作流程正常工作,您需要申请授权码(通过OAuth规范第4.1.1段中提到的授权请求)。
从OAuth2Consumer
类调用该方法:
public String generateRequestAuthorizationUrl(ResponseType responseType, String redirectUri, String state, String scopeDelimiter, String... scope) throws OAuthException {
请记住,scopeDelimiter
for Facebook是逗号,
而responseType
是ResponseType.CODE
。 scope
是Facebook认为的权限。
一个完整的例子是:
String client_id = "<APP_ID>";
String client_secret = "<CLIENT_SECRET>";
String redirectUrl = "http://127.0.0.1:5080/Red5FacebookAuth/oauth/oauth_redirect";
OAuth2ServiceProvider provider = new OAuth2ServiceProvider("https://graph.facebook.com/oauth/authorize", "https://graph.facebook.com/oauth/access_token");
OAuth2Consumer consumer = new OAuth2Consumer(client_id, client_secret, provider);
//Using HttpServletResponse (but you can kickstart it through an Action/Controller/etc.
response.sendRedirect(consumer.generateRequestAuthorizationUrl(ResponseType.CODE, redirectUrl, null, ",", (String[])null)); //where null is the scope array,
当Facebook执行HTTP重定向时,这将依次调用com.facebook.FacebookOAuthService.processReceivedAuthorization
。然后code
是您从Facebook收到的授权码。
希望这有帮助!
PS Facebook在请求访问令牌时不会对您的页面执行HTTP重定向,因此您手动将其存储在会话中而不是JOAuth(它使用OAuth 2草案0) 。如果其他OAuth 2服务提供商在请求访问令牌后使用HTTP重定向,不存储访问令牌,OAuth2Service
会自动为您执行此操作。
PPS 使用任何日志框架查看日志。