原创

在 Postman 中工作的 API 调用在 Java 中不起作用,重定向似乎是问题所在

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I have 2 API Calls, these have been tested and work perfectly using postman. I have used the sessionToken retrieved from the 1st call in java and used it in postman to execute second api call and that works.

1st retrieves a sessionToken, which is passed to second call as a parameter. 2nd call has redirects but should end up on a page where I can Grab a SAMLResponse.

Below is the code and I believe the issue is how java is handling the redirects (the only thing I can think of) but I do not know how to fix the issue. (PS I have tried a couple other java http libraries as well with same issue)

Any Ideas about what I am doing wrong here?

        client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_1_1)
                .followRedirects(Redirect.NORMAL)
                .build();

        ApiURL = "https://company.okta.com/api/v1/authn";
        jsonBody = new JSONObject("{\"username\": \"" + userNameString + "\",\"password\": \"" + userPasswordString + "\",\"options\": {\"multiOptionalFactorEnroll\": false,\"warnBeforePasswordExpired\": false}}");

        request = HttpRequest.newBuilder()
                .uri(URI.create(ApiURL))
                .header("accept", "*/*")
                .header("content-type", "application/json")
                .method("POST", BodyPublishers.ofString(jsonBody.toString()))
                .build();

        response = client.send(request, BodyHandlers.ofString());

        String cookieHeader = "";
        if (response.statusCode() == 200) {
            jsonBody = new JSONObject(response.body());
            sessionToken = jsonBody.getString("sessionToken");
        }

        ApiURL = "https://company.okta.com/home/company_application_1/0oa2v2ll39RRbl5ae1t7/aln2v2sgr83LTglE71t7";
        fullUrl = String.format("%s?sessionToken=%s", ApiURL, sessionToken);

        request = HttpRequest.newBuilder()
                .uri(URI.create(fullUrl))
                .header("accept", "*/*")
                .method("POST", BodyPublishers.noBody())
                .build();

        response = client.send(request, BodyHandlers.ofString());

I am expectiong to be sent to the application landing page where i can grab the SAMLResponse value. But this is sending me to the okta login screen instead.

This works perfectly in postman.

I have tried to manually code through the redirects with same results (there are 2 redirects that happen)

I have tried a couple other java http libraries as well with same issue

I believe the issue is how java is handling the redirects (the only thing I can think of) but I do not know how to fix the issue.

正文到此结束
热门推荐
本文目录