1

We are trying to use Exact Online API. It is using Apache OAuth 2.0 framework. For that we followed the below document.

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

I successfully able to get the authorization code but failing to get the access_token with exception like below.

OAuthProblemException{error='invalid_request', description='Missing parameters: access_token', uri='null', state='null', scope='null', redirectUri='null', responseStatus=0, parameters={}}

My code is like this.

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

Can some one please help me to sort this out.

Guido Leenders
  • 4,232
  • 1
  • 23
  • 43
Purna
  • 43
  • 7
  • Redirect URI can't be localhost I guess. – Roshith Jul 28 '15 at 07:09
  • Redirect URI is nothing but an callback response handler from the Exact web service. Here in my case it is nothing but a servlet which is running in the same machine. – Purna Jul 29 '15 at 17:21

1 Answers1

2

Finally i resolved this issue with a simple change. The problem is with the line

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

Instead of this we have to use either of the below lines to get the access token properly.

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

(Or)

OAuthAccessTokenResponse oAuthResponse =oAuthClient.accessToken(oAuthrequest,OAuth.HttpMethod.POST);
Purna
  • 43
  • 7
  • Could you please guide on https://stackoverflow.com/questions/47001135/apache-oltu-instragram-integration-doesnt-return-access-token-why ? – PAA Oct 29 '17 at 14:32