获取微信稳定版接口调用凭证报错 412 Precondition Failed , 是咋回事呢?
// org.springframwork.web.cleint.HttpClientErrorException: 412 Precondition Failed
API: https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/getStableAccessToken.html
代码如下:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AccessTokenParam {
/**
* 固定值:client_credential
*/
private String grant_type;
/**
* APPID
*/
private String appid;
/**
* secret
*/
private String secret;
}
AccessTokenParam accessTokenParam = AccessTokenParam.builder()
.grant_type(GRANT_TYPE)
.appid(appID)
.secret(accountSecret)
.build();
// 该行报错 org.springframwork.web.cleint.HttpClientErrorException: 412 Precondition Failed
String res = restTemplate.postForObject(URL, accessTokenParam, String.class);
你好,截图下报错呢?
大概是因为微信服务器不支持 Chunked Transfer Coding 请求,而客户端发的请求体却是 Transfer-Encoding: chunked 编码所致。
可以试试在配置 RestTemplateBuilder 时,将 requestFactory 用 BufferingClientHttpRequestFactory 包一下。
参考资料:https://github.com/spring-projects/spring-framework/issues/31797#issuecomment-1850081007