将 curl http 请求转换为 Java 的错误请求
Bad request converting curl http request to Java
我有以下 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'
这是抛出 Bad Request 异常的 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;
}
我也试过将 --data 部分添加到参数对象中,但我遇到了同样的问题。我正在使用 RestTemplate,但我愿意接受其他建议。
感谢您的帮助。
我想问题在于,在 curl
示例中,您在 POST body 中传递了这些参数,而在您的 java 代码中,您使用 headers反而。尝试将其更改为使用 body params of entity
object:
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());
您需要在格式为 url 编码的请求实体中发送这些参数,并将 content-type 设置为 application/x-www-form-urlencoded
。
你的 body 可以是一个字符串(根据你的例子):
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);
设置内容类型header:
headers.add("Content-Type", "application/x-www-form-urlencoded");
(实际实现取决于您使用的库)
我有以下 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'
这是抛出 Bad Request 异常的 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;
}
我也试过将 --data 部分添加到参数对象中,但我遇到了同样的问题。我正在使用 RestTemplate,但我愿意接受其他建议。
感谢您的帮助。
我想问题在于,在 curl
示例中,您在 POST body 中传递了这些参数,而在您的 java 代码中,您使用 headers反而。尝试将其更改为使用 body params of entity
object:
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());
您需要在格式为 url 编码的请求实体中发送这些参数,并将 content-type 设置为 application/x-www-form-urlencoded
。
你的 body 可以是一个字符串(根据你的例子):
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);
设置内容类型header:
headers.add("Content-Type", "application/x-www-form-urlencoded");
(实际实现取决于您使用的库)