简体   繁体   中英

Token request on Exact Online not returning access token

I have been trying to use OAuth 2.0 to connect to Exact Online. We tend to focus on Java-applications and sadly Exact doesn't have documentation/examples/support for Java.

I was able to do the authentication request but for the token request I'm having some trouble. My code:

OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
        code = oar.getCode();

        OAuthClientRequest oAuthRequest;
        try {
            oAuthRequest = OAuthClientRequest
                    .tokenLocation("https://start.exactonline.be/api/oauth2/token")
                    .setGrantType(GrantType.AUTHORIZATION_CODE)
                    .setClientId(CLIENT_ID)
                    .setClientSecret(CLIENT_SECRET)
                    .setRedirectURI(REDIRECT_URI)
                    .setCode(code)
                    .buildQueryMessage();


            OAuthClient client = new OAuthClient(new URLConnectionClient());

            OAuthJSONAccessTokenResponse oauthResponse = client.accessToken(oAuthRequest, OAuth.HttpMethod.POST);
        }

I've looked around but all the answers I've found didn't solve the problem.

  • Tried to use the other tokenresponse types.
  • Tried using .buildBodyMessage instead of .buildQueryMessage

I always get 1 of these ProblemExceptions:

Token request failed: unsupported_response_type, Invalid response! Response body is not application/json encoded

Token request failed: invalid_request, Missing parameters: access_token

I was hoping anyone has prior experience dealing with exact online, any help is appreciated.

Not sure how the Java library works, I have used Exact Online on C# only.

The problem seems to be that the library tries to extract the access_token from the JSON response, but there is no JSON in the response. It is possible that you didn't authorize your application yet (a user action) or you are using the wrong return URL value (Exact checks it server side). You might want to look to the actual output (I guess HTML) if possible. The real exception should be in there.

Solution was to use an older version of the Oltu library. Using 0.31 works, but 1.0.1 does not. Strange behavior but maybe this will help someone in the future.

 .tokenLocation("https://start.exactonline.be/api/oauth2/token")
                .setGrantType(GrantType.AUTHORIZATION_CODE)
                .setClientId(CLIENT_ID)
                .setClientSecret(CLIENT_SECRET)
                .setRedirectURI(REDIRECT_URI)
                .setCode(code)
                .buildBodyMessage();

When changing .buildQueryMessage() to .buildBodyMessage() , it worked for me.

This is the latest dependency:

    <dependency>
        <groupId>org.apache.oltu.oauth2</groupId>
        <artifactId>org.apache.oltu.oauth2.client</artifactId>
        <version>1.0.2</version>
    </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM