如何在java接听电话时添加身体
我正在尝试从Auth Server获取令牌,Auth Server提供的获取API,该API需要在身体中以下参数,即客户ID和客户端秘密Grant_Type。
我尝试了Springs RestTemaplte,Springboot的Webclient以及Okhttp。 我仍然无法使用http get /doken方法通过身体。
我尝试使用OKHTTP,但要以下内容错误
java.lang.IllegalArgumentException: method GET must not have a request body.
okhttpclient client = new okhttpclient().newbuilder()。build()。build();
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("grant_type", "client_credentials")
.addFormDataPart("client_id", "9eb43929-1212-41e8-92bf-12313131").addFormDataPart("client_secret", "7RD7Q~1313131~HFBE2DoXR37zrcLCdpLnP")
.addFormDataPart("resource", "eb63d611-525e-4a31-1212-0cb33f679599").build();
Request request = new Request.Builder().url("https://host/oauth2/token/").method("GET", body).build();
Response response = client.newCall(request).execute();
I am trying to get token from auth server, Auth server provided GET API which require following parameters in body i.e. client id and client secret , grant_type.
I tried with springs resttemaplte, webclient from springboot as well as okhttp .
still i am not able to pass body with HTTP GET /token method.
i tried with okhttp but getting following error
java.lang.IllegalArgumentException: method GET must not have a request body.
OkHttpClient client = new OkHttpClient().newBuilder().build();
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("grant_type", "client_credentials")
.addFormDataPart("client_id", "9eb43929-1212-41e8-92bf-12313131").addFormDataPart("client_secret", "7RD7Q~1313131~HFBE2DoXR37zrcLCdpLnP")
.addFormDataPart("resource", "eb63d611-525e-4a31-1212-0cb33f679599").build();
Request request = new Request.Builder().url("https://host/oauth2/token/").method("GET", body).build();
Response response = client.newCall(request).execute();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OKHTTP不支持带有尸体的请求。您需要更改您使用的HTTP方法,或使用其他HTTP客户端。
OkHttp does not support GET requests with bodies. You'll need to either change which HTTP method you’re using, or use a different HTTP client.
您确定帖子方法无法正常工作吗?我不知道为什么任何验证服务器都需要使用GET方法,因为违反了REST标准惯例
Are you sure that a POST method won't work? I don't know why any auth server would require a body using a GET method since that violates REST standard convention