Spring Boot RestTemplate:直接从邮递员复制时出现错误请求

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

所以我有一个API请求,我直接在其中从邮递员那里复制详细信息。但是,我收到一个错误的请求错误。

@Service
public class GraphApiService {

@Bean
public RestTemplate restTemplate() {

    return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@Autowired
Constants constants;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public ResponseEntity<String> getAccessTokenUsingRefreshToken(Credential cred) throws IOException{
    try {

    //https://docs.microsoft.com/en-us/graph/auth-v2-user
    // section 5. Use the refresh token to get a new access token
    String url = "url";
    JSONObject body = new JSONObject();
    body.put("grant_type", "refresh_token");
    body.put("client_id", "clientid");
    body.put("scope","User.Read offline_access Files.Read Mail.Read Sites.Read.All");
    body.put("redirect_uri", "http://localhost");
    body.put("client_secret","secret");
    body.put("refresh_token", "token");
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<String> request =  new HttpEntity<String>(body.toString(), headers);
   ResponseEntity<String> response=  restTemplate.postForEntity(url, request,String.class);
   return response;
    }
    catch(HttpClientErrorException  e){
        logger.error(e.getResponseBodyAsString());
        logger.error(e.getMessage());
        return null;
    }

}

我将不胜感激。来自Microsoft图形的错误请求错误消息不是描述性消息,将帮助您]

所以我有一个API请求,我直接在其中从邮递员那里复制详细信息。但是,我收到了错误的请求错误。 @Service公共类GraphApiService {@Bean public ...

spring-boot resttemplate
1个回答
1
投票

您正在发送带有FORM_URLENCODED标头的JSON有效负载。您需要检查API是否接受json负载,如果需要,则需要将content-type更改为application/json,或者可以按以下方式发布表单数据。

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