向Spring RestTemplate转换的Curl令牌请求

问题描述 投票:0回答:2

我正在尝试将curl请求转换为Spring的Resttemplate post请求,但是却收到403-错误的请求

我的卷曲请求

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

卷曲响应:

{"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对于该问题几乎没有修改。

java spring curl resttemplate
2个回答
0
投票

0
投票

您的卷发适用于

© www.soinside.com 2019 - 2024. All rights reserved.