如何在java接听电话时添加身体

发布于 2025-02-08 07:57:09 字数 928 浏览 0 评论 0原文

我正在尝试从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2025-02-15 07:57:10

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.

謌踐踏愛綪 2025-02-15 07:57:10

您确定帖子方法无法正常工作吗?我不知道为什么任何验证服务器都需要使用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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文