我正在尝试转换一个 curl 请求,它给我一个 token 到 Spring 的 Resttemplate post 请求中,但我收到 403 - 错误请求
我的 curl 请求
curl -d "grant_type=client_credentials&client_id=nu5yzeth9tektzf5egxuntp7&client_secret=uP2Xvr6SCKYgXgxxJsv2QkUG"
-H "Content-Type: application/x-www-form-urlencoded"
-X POST https://cloudsso.example.com/as/token.oauth2
curl 响应:
{"access_token":"HVURQ845OPJqs8UpOlef5m2ZCNwR","token_type":"Bearer","expires_in":3599}
现在,这是我实现上述 curl post 请求的 Java 代码
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> bodyParamMap = new LinkedMultiValueMap<String, String>();
bodyParamMap.add("grant_type", "client_credentials");
bodyParamMap.add("client_id", "nu5yzeth9tektzf5egxuntp7");
bodyParamMap.add("client_secret", "uP2Xvr6SCKYgXgxxJsv2QkUG");
entity = new HttpEntity<>(reqBodyData, bodyParamMap);
restTemplate.postForEntity(url, entity, TokenDTO.class)
Resttemplate调用响应:
2019-12-04 11:10:16,483[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Accept=[application/json, application/*+json]
[30m2019-12-04 11:10:16,484[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Writing [{"grant_type":["client_credentials"],"client_id":["nu5yzeth9tektzf5egxuntp7"],"client_secret":["uP2Xvr6SCKYgXgxxJsv2QkUG"]}] with org.springframework.http.converter.StringHttpMessageConverter
[30m2019-12-04 11:10:17,976[0;39m [39mDEBUG[0;39m [[34mrestartedMain[0;39m] [33morg.springframework.core.log.CompositeLog[0;39m: Response 400 BAD_REQUEST
[30m2019-12-04 11:10:17,981[0;39m [34mINFO [0;39m [[34mrestartedMain[0;39m] [33mcom.ibm.ciscoApiIntegration.service.impl.CiscoAPIServiceImpl[0;39m: e::: 400 Bad Request
如何使 RestTemplate
中的上述 curl 请求正常工作?
备注:https://cloudsso.example.com/as/token.oauth2对于这个问题几乎没有修改。
最佳答案
如果您查看 HttpEntity
的文档你会发现你使用了错误的构造函数。
entity = new HttpEntity<>(reqBodyData, bodyParamMap);
您正在传递要用作正文 (bodyParamMap
) 的参数作为 header (因为第二个参数是要用于请求的 header )。事实上,您甚至没有为请求使用 HttpHeaders
,因为您没有将它们传递给 HttpEntity
。我不确定 reqBodyData
是什么,但可能是 bodyParamMap
的 toString
。
你应该做的是下面的
entity = new HttpEntity<>(bodyParamMap, headers);
作为 HttpHeaders
impements MultiValueMap
,您可以直接在构造函数中使用它们。您还想按原样传递 bodyParamMap
。
注意:我还建议在 HttpHeaders
上明确设置 Content-Type
,这样您就可以确定发送的是什么。
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
因此总的结果代码应该是这样的
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> bodyParamMap = new LinkedMultiValueMap<>();
bodyParamMap.add("grant_type", "client_credentials");
bodyParamMap.add("client_id", "nu5yzeth9tektzf5egxuntp7");
bodyParamMap.add("client_secret", "uP2Xvr6SCKYgXgxxJsv2QkUG");
entity = new HttpEntity<>(bodyParamMap, headers);
restTemplate.postForEntity(url, entity, TokenDTO.class)
注意:您不应该构造一个单独使用的 RestTemplate
,而是您希望配置一次并将其注入(inject)到您的类中。
关于java - Curl Token 请求到 Spring RestTemplate 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59169912/