在 Postman 中工作的 API 调用在 Java 中不起作用,重定向似乎是问题所在
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.
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2372
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权