将curl http请求转换为Java的请求错误

将curl http请求转换为Java的请求错误,java,rest,http,curl,resttemplate,Java,Rest,Http,Curl,Resttemplate,我有以下与curl的请求,该请求与Microsoft Azure服务对话没有问题 curl --request POST https://login.microsoftonline.com/common/oauth2/v2.0/token --data 'client_id=fe37...06-566f5c762ab2&grant_type=authorization_code&client_secret=tPv..dQfqomaG&scope=mail.read&

我有以下与curl的请求,该请求与Microsoft Azure服务对话没有问题

curl --request POST https://login.microsoftonline.com/common/oauth2/v2.0/token --data 'client_id=fe37...06-566f5c762ab2&grant_type=authorization_code&client_secret=tPv..dQfqomaG&scope=mail.read&code=OAQABAAIA...gAA'
以下是引发错误请求异常的java代码:

 public String getToken(String authCode){

        try {

            HttpHeaders headers = new HttpHeaders();

            String url = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
            UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
            headers.add("client_id", "fe3..b2");
            headers.add("client_secret", "tP..aG");
            headers.add("grant_type", "authorization_code");
            headers.add("code", authCode);
            headers.add("scope", "mail.read");


            HttpEntity<?> entity = new HttpEntity<>(headers);
            RestTemplate restTemplate = new RestTemplate();

            HttpEntity<String> response = restTemplate.exchange(builder.build().toUri(), HttpMethod.POST, entity, String.class);


        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
publicstringgettoken(stringauthcode){
试一试{
HttpHeaders=新的HttpHeaders();
字符串url=”https://login.microsoftonline.com/common/oauth2/v2.0/token";
UriComponentsBuilder=UriComponentsBuilder.fromHttpUrl(url);
标题。添加(“客户id”、“fe3..b2”);
标题。添加(“客户机密”、“tP..aG”);
标题。添加(“授权类型”、“授权代码”);
添加(“代码”,authCode);
headers.add(“scope”、“mail.read”);
HttpEntity=新的HttpEntity(标题);
RestTemplate RestTemplate=新RestTemplate();
HttpEntity响应=restTemplate.exchange(builder.build().toUri(),HttpMethod.POST,entity,String.class);
}
捕获(例外e){
e、 printStackTrace();
}
返回null;
}
我还尝试将--data部分添加到parameters对象中,我收到了相同的问题。我正在使用RestTemplate,但我愿意接受其他建议


我很感激您的帮助。

我想问题在于,在
curl
示例中,您将这些参数传递到POST正文中,而在java代码中,您使用的是标题。尝试将其更改为使用
实体的body参数
对象:

MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();     

body.add("client_id", "fe3..b2");
// ... rest params

// Note the body object as first parameter!
HttpEntity<?> entity = new HttpEntity<Object>(body, new HttpHeaders());
MultiValueMap body=新链接的MultiValueMap();
正文。添加(“客户id”、“fe3..b2”);
// ... 休息参数
//请注意,主体对象是第一个参数!
HttpEntity实体=新的HttpEntity(主体,新的HttpHeaders());

您需要在格式为form url encoded的请求实体中发送这些参数,并将内容类型设置为
application/x-www-form-urlencoded

您的主体可以是字符串(根据您的示例):

(实际实现取决于您使用的库)

String data = "client_id=fe37...06-566f5c762ab2&grant_type=authorization_code&client_secret=tPv..dQfqomaG&scope=mail.read&code=OAQABAAIA...gAA";
HttpEntity<String> entity = new HttpEntity<>(data);
headers.add("Content-Type", "application/x-www-form-urlencoded");