代码之家  ›  专栏  ›  技术社区  ›  vicky

Curl令牌请求到Spring restemplate转换

  •  0
  • vicky  · 技术社区  · 5 年前

    我的卷发请求

    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}
    

    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 工作?

    注: https://cloudsso.example.com/as/token.oauth2

    2 回复  |  直到 5 年前
        1
  •  2
  •   M. Deinum    5 年前

    如果你看一下文档 HttpEntity

    entity = new HttpEntity<>(reqBodyData, bodyParamMap);
    

    bodyParamMap )作为头(因为第二个参数是用于请求的头)。事实上,你甚至没有使用 HttpHeaders 因为你没有把它们传给 HttpEntity公司 . 我不知道这是怎么回事 reqBodyData toString 体参数图 .

    你应该做的是

    entity = new HttpEntity<>(bodyParamMap, headers);
    

    HttpHeaders文件 弹劾 MultiValueMap 您可以直接在构造函数中使用它们。你还想通过考试吗 体参数图 照原样。

    注: 我还建议明确设置 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

        2
  •  1
  •   Shivam Puri    5 年前

    https://stackoverflow.com/a/49127760/11226302

    您应该将头中的请求内容类型设置为; headers.setContentType文件(MediaType.APPLICATION\表单\URLENCODED);

    希望这有帮助!

        3
  •  1
  •   vicky    5 年前

    我刚刚解决了这个问题:

    // wrong
    //private HttpEntity<String> entity;
    

    我改成这样:

    private MultiValueMap<String, String> parametersMap;
    

    改变了它

    // not working
    entity = new HttpEntity<>(reqBodyData, bodyParamMap);
    

    为了这个

    // working
    entity = new HttpEntity<>(bodyParamMap, headers);
    

    现在它工作得很好。

    答复:

    2019-12-04 11:52:46,503 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: HTTP POST https://cloudsso.example.com/as/token.oauth2
    2019-12-04 11:52:46,521 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Accept=[application/json, application/*+json]
    2019-12-04 11:52:46,522 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Writing [{grant_type=[client_credentials], client_id=[nu5yzeth9tektzf5egxuntp7], client_secret=[uP2Xvr6SCKYgXgxxJsv2QkUG]}] with org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter
    2019-12-04 11:52:47,831 DEBUG [restartedMain] org.springframework.core.log.CompositeLog: Response 200 OK
    

    //headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);