Search code examples
javaspringspring-bootspring-cloud-feignopenfeign

OpenFeign not decoding response


@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
    );
}

Call:

        Token token = authorizationApi.getToken(
                "authorization_code",
                code,
                REDIRECT_URI,
                getAuthorizationCredentials(),
                "porymol");
        System.out.println(token.access_token()); //returns null

Token record:

public record Token(String access_token, String token_type, Long expires_in, String scope, String refresh_token) {
}

When I make request from Postman I get this response:

{
    "access_token": "token",
    "token_type": "bearer",
    "expires_in": 86400,
    "refresh_token": "token",
    "scope": "read"
}

Trying to get any value from Token returns null

Java 17, Spring boot 3

Any idea what's going wrong here?


Solution

  • First of all you have declared two path variables which dont show up the path: @PathVariable String code and @PathVariable("redirect_uri") String redirectUrl.

    Overall it looks like you are trying to request an oauth access token which requires an request of content type application/x-www-form-urlencoded.

    Maybee this helps: How to POST form-url-encoded data with Spring Cloud Feign