@FeignClient(name = "Authorization-API", url = "https://www.reddit.com/api/v1")
public interface AuthorizationApi {
@RequestMapping(method = RequestMethod.POST, value = "/access_token")
Token getToken(@PathVariable("grant_type") String grantType,
@PathVariable String code,
@PathVariable("redirect_uri") String redirectUrl,
@RequestHeader(name = "Authorization") String authorizationHeader,
@RequestHeader("User-agent") String agent
);
}
调用:
Token token = authorizationApi.getToken(
"authorization_code",
code,
REDIRECT_URI,
getAuthorizationCredentials(),
"porymol");
System.out.println(token.access_token()); //returns null
标记记录:
public record Token(String access_token, String token_type, Long expires_in, String scope, String refresh_token) {
}
当我向Postman发出请求时,我得到了这样的响应:
{
"access_token": "token",
"token_type": "bearer",
"expires_in": 86400,
"refresh_token": "token",
"scope": "read"
}
尝试从Token中获取任何值返回null
Java 17, Spring boot 3
你知道这里出了什么问题吗?
首先你声明了两个不显示路径的路径变量:@PathVariable String code
和@PathVariable("redirect_uri") String redirectUrl
.
总的来说,看起来你正在尝试请求一个oauth访问令牌,它需要一个内容类型为application/x-www-form-urlencoded
的请求。
也许这有帮助:如何使用Spring Cloud Feign POST表单url编码的数据