Exact Online 上的令牌请求未返回访问令牌

Token request on Exact Online not returning access token

我一直在尝试使用 OAuth 2.0 连接到 Exact Online。我们倾向于关注 Java-应用程序,遗憾的是 Exact 没有 documentation/examples/support 用于 Java。

我能够执行身份验证请求,但对于令牌请求我遇到了一些问题。我的代码:

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

我环顾四周,但我找到的所有答案都没有解决问题。

我总是遇到以下 ProblemExceptions 中的 1 个:

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

Token request failed: invalid_request, Missing parameters: access_token

我希望任何人都有过处理精确在线的经验,我们将不胜感激。

不确定 Java 库是如何工作的,我只在 C# 上使用过 Exact Online。

问题似乎是库试图从 JSON 响应中提取 access_token,但响应中没有 JSON。您可能还没有授权您的应用程序(用户操作)或者您使用了错误的 return URL 值(Exact 检查它的服务器端)。如果可能,您可能希望查看实际输出(我猜 HTML)。真正的例外应该在那里。

解决方案是使用旧版本的 Oltu 库。使用 0.31 有效,但 1.0.1 无效。奇怪的行为,但也许这对将来的人有帮助。

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

.buildQueryMessage() 更改为 .buildBodyMessage() 时,对我有用。

这是最新的依赖项:

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