【发布时间】:2020-11-19 07:29:12
【问题描述】:
我有以下返回 access_token 的 API。
发布https://idcs-xxxxxxxxxbf08128c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token
在标题 content-type is application/x-www-form-urlencoded 中。在正文中也包含以下参数。
我发送用户名和密码,并通过基本身份验证进行保护。当我从邮递员那里打电话时,它会提供 access_token。当我使用 HttpUrlConnection 消费时它也提供输出
url = new URL(tokenURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization", auth);
connection.setRequestProperty("Accept", "application/json");
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("grant_type=client_credentials&scope=" + scope);
以上代码运行正常。但是当我使用球衣时,它会出现 415 错误。我正在使用下面的代码。
String user="idcs-oda-zzzxxxxxf93560b94eb8a2e2a4c9aac9a3ff-t0_APPID";
String password="xxxxxxx-6f71-4af2-b5cc-9110890d1456";
String scope = "https://idcs-oda-xxxxxxxxxxxxxxxxe2a4c9aac9a3ff-t0.data.digitalassistant.oci.oc-test.com/api/v1";
String tokenURL = "https://idcs-xxxxxxxxxxxxxxxx28c3d93a19c.identity.c9dev2.oc9qadev.com/oauth2/v1/token";
HttpAuthenticationFeature feature= HttpAuthenticationFeature
.basicBuilder()
.nonPreemptive()
.credentials(user,password)
.build();
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(feature);
Client client = ClientBuilder.newClient(clientConfig);
WebTarget webTarget= client.target(tokenURL);
PostDetails post= new PostDetails("client_credentials",scope); //Bean class to assign body parameter
Response response= webTarget.request()
.header("Content-Type", "application/x-www-form-urlencoded")
.post(Entity.json(post));
System.out.println(response);
谁能告诉我我在响应行中犯了什么错误。
【问题讨论】:
-
请编辑您的existing question 而不是创建重复项
-
其实我这里已经给出了更多的细节。我会删除旧的。你能帮忙吗?我真的很挣扎
-
webTarget.request("application/x-www-form-urlencoded") 这是干什么用的?
-
你没有设置这个connection.setRequestProperty("Accept", "application/json");为什么?
-
webTarget.request() .header("Content-Type", "application/x-www-form-urlencoded") .header('Accept','application/x-www-form- urlencoded') .post(Entity.json(post));
标签: java jersey jax-rs jersey-2.0 jersey-client