1

我们正在尝试使用 Exact Online API。它使用 Apache OAuth 2.0 框架。为此,我们遵循了以下文件。

https://developers.exactonline.com/#OAuth_Tutorial.html%3FTocPath%3DAuthentication%7C_____2

我成功地获得了授权码,但未能获得 access_token 并出现如下异常。

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();
        }
    }

有人可以帮我解决这个问题。

4

1 回答 1

2

最后我通过一个简单的改变解决了这个问题。问题出在线路上

  GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(oAuthrequest, "POST",GitHubTokenResponse.class);

取而代之的是,我们必须使用以下任一行来正确获取访问令牌。

 OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.accessToken(oAuthrequest, OAuth.HttpMethod.POST);

(或者)

OAuthAccessTokenResponse oAuthResponse =oAuthClient.accessToken(oAuthrequest,OAuth.HttpMethod.POST);
于 2015-07-29T17:04:29.707 回答