Home > Mobile >  How to get a Token in SpringBoot form external Site: Thymeleaf Error
How to get a Token in SpringBoot form external Site: Thymeleaf Error

Time:04-09

i want to get a token in my spring-boot project from a external website like spotify.com to consume data.

when i call the endpoint i get the token and the error message below.

[HttpClient-1-Worker-0] [588ms] SSL Reader(SocketTube(1)) upstreamWindowUpdate, downstreamQueueSize:0, upstreamWindow:1
[  XNIO-1 task-1] org.thymeleaf.TemplateEngine : [THYMELEAF][XNIO-1 task-1] Exception processing template "{"access_token":"xxx[...]arer","scope":"read","refresh_token":"xxx"}": Error resolving template [{"access_token":"xxx","expires_in":xxx,"token_type":"Bearer","scope":"read","refresh_token":"xxx"}], template might not exist or might not be accessible by any of the configured Template Resolvers

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [{"access_token":"xxx","expires_in":xxx,"token_type":"Bearer","scope":"read","refresh_token":"xxx"}], template might not exist or might not be accessible by any of the configured Template Resolvers

i dont want to use any kind of tamplate, its just only a request. is there a better way to do this? sadly i am not familar with thymeleaf.

Implementation:

/**
     * {@code POST  /tokens/o-auth} : Create a new token from Cryptohopper.
     *
     * @param codeDto the tokenDTO to create.
     */
    @PostMapping("/tokens/o-auth")
    @ResponseStatus(HttpStatus.OK)
    public Object createTokenFromCryptoHopper(@RequestBody CodeDto codeDto) throws IOException, InterruptedException {

        var request1 = new HashMap<String, String>() {{
            put("grant_type", codeDto.getGrant_type());
            put("code", codeDto.getCode());
            put("client_id", codeDto.getClient_id());
            put("client_secret", codeDto.getClient_secret());
            put("redirect_uri", codeDto.getRedirect_uri());
        }};

        var objectMapper = new ObjectMapper();
        String requestBody = objectMapper
            .writeValueAsString(request1);

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://www.cryptohopper.com/oauth2/token"))
            .POST(HttpRequest.BodyPublishers.ofString(requestBody))
            .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.body());
        return response.body();
    }


public class TokenDTO implements Serializable {
    private String access_token;
    private String expires_in;
    private String token_type;
    private String scope;
    private String refresh_token;
}


public class CodeDto implements Serializable {
    private String grant_type;
    private String code;
    private String client_id;
    private String client_secret;
    private String redirect_uri;
}

best regards

CodePudding user response:

Do you have your controller annotated with @Controller or @RestController? The error message looks like your json string in your response is interpreted as a template name which hints the first one.

If you want to return json you should 'announce' that so with.
@PostMapping(path="/tokens/o-auth", produces = MediaType.APPLICATION_JSON_VALUE)

You may want to use ResponseEntity as a return type instead of plain Object

  • Related