Oauth 如何使用ApacheAuth 2.0获取准确在线API的访问令牌
我们正在尝试使用精确的在线API。它使用的是ApacheAuth2.0框架。为此,我们遵循以下文件 我成功地获得了授权码,但未能获得访问令牌,异常如下Oauth 如何使用ApacheAuth 2.0获取准确在线API的访问令牌,oauth,access-token,exact-online,Oauth,Access Token,Exact Online,我们正在尝试使用精确的在线API。它使用的是ApacheAuth2.0框架。为此,我们遵循以下文件 我成功地获得了授权码,但未能获得访问令牌,异常如下 OAuthProblemException{error='invalid_request', description='Missing parameters: access_token', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0,
OAuthProblemException{error='invalid_request', description='Missing parameters: access_token', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}
我的代码是这样的
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
String code = oar.getCode();
OAuthClientRequest oAuthrequest = OAuthClientRequest
.tokenLocation("https://start.exactonline.co.uk/api/oauth2/token")
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId("my client id")
.setClientSecret("my client secret")
.setRedirectURI("http://localhost:8080/SampleServlet/AuthServlet")
.setCode(code)
.buildBodyMessage();
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(oAuthrequest, "POST",GitHubTokenResponse.class);
out.println("Access Token = " + oAuthResponse.getAccessToken());
} catch (OAuthSystemException ex) {
Logger.getLogger(AuthServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (OAuthProblemException ex) {
Logger.getLogger(AuthServlet.class.getName()).log(Level.SEVERE, null, ex);
} finally {
out.close();
}
}
有人能帮我解决这个问题吗。最后,我通过一个简单的改动解决了这个问题。问题出在线路上
GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(oAuthrequest, "POST",GitHubTokenResponse.class);
取而代之的是,我们必须使用以下任一行来正确获取访问令牌
OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oAuthrequest, OAuth.HttpMethod.POST);
(或)
我猜重定向URI不能是localhost。重定向URI只是来自确切web服务的回调响应处理程序。在我的例子中,它只不过是一个在同一台机器上运行的servlet。请您指导一下好吗?
OAuthAccessTokenResponse oAuthResponse =oAuthClient.accessToken(oAuthrequest,OAuth.HttpMethod.POST);