为什么在执行确切的oauth时获取oauthproblemexception error='invalid\u request'description='handle cannot be extracted'

ru9i0ody  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(471)

在调用exact-online-api进行身份验证时,我们遇到了获取第一个刷新令牌失败的问题。我们不知道为什么。这是我们从exact得到的信息:
http代码:400
json数据:

{
    error='invalid_request',
    description='Handle could not be extracted',
    uri='null',
    state='null',
    scope='null',
    redirectUri='null',
    responseStatus=400,
    parameters={}
}

我们使用基于库org.apache.oltu.oauth2.client(1.0.2)的java代码:

OAuthClientRequest oAuthRequest = OAuthClientRequest //
        .tokenLocation(BASE_URL + "/api/oauth2/token") //
        .setGrantType(GrantType.AUTHORIZATION_CODE) //
        .setClientId(clientId) //
        .setClientSecret(clientSecret) //
        .setRedirectURI(REDIRECT_URI) //
        .setCode(code) //
        .buildBodyMessage();

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

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

我们确实使用localhost重定向执行了第一步(获取setcode(…)中使用的“code”),如中所示https://support.exactonline.com/community/s/knowledge-base#all-所有dno内容都从那里开始我们从浏览器的地址栏复制代码,并将其存储在下一步计算机可以再次读取的地方。

z9ju0rcb

z9ju0rcb1#

这是因为代码是从浏览器地址栏复制的。在这里,您将发现一个url编码的代码版本(通常在“%21”中可见),当它被逐字传递到setcode中时,后续调用将失败。
建议:url解码该值或使用undertow或类似工具设置一个小型临时localhost http服务器,以捕获发送给您的代码localhost url:

Undertow server = Undertow.builder() //
        .addHttpListener(7891, "localhost") //
        .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                String code = exchange.getQueryParameters().get("code").getFirst();
                LOG.info("Recieved code: {}.", code);
                LOG.info("Store code");
                storeCode(code);
                LOG.info("Code stored");

                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
                exchange.getResponseSender().send( //
                        "Thanks for getting me the code: " + code + "\n" //
                                + "Will store it for you and get the first refreshToken..." //
                                + "Please have a look at " + OAUTH_STATE_INI
                                + " for the new code & refreshToken in a minute" //
                );

                done.add("done");
            }
        }).build();
server.start();

注意:一定要确保重定向网址是正确的,在您的确切应用程序设置

相关问题