Problem z połączeniem się do api

0

Siema, próbuje się połączyć do następującego api https://anapioficeandfire.com Niestety ciągle dostaje komunikat 403 Forbidden. Z dokumentacji nie wynika że potrzebuje jakiegoś klucza. Może ktoś wie jak to ogarnąć.

 @Override
    public String testApi() {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> httpEntity = new HttpEntity<>("parameters", httpHeaders);
        ResponseEntity<String> x = restTemplate.exchange("https://www.anapioficeandfire.com/api/characters", HttpMethod.GET, httpEntity, String.class);

        return x.getBody();
    }
1

U mnie twój kod nie działał, znalazłem wątek na StackOverflow że jest to problem z RestTemplate, po dodaniu user-agenta w nagłówkach u mnie działa. Kod sklejony na szybko poniżej:

    public static String testApi() throws Exception {

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> httpEntity = new HttpEntity<>("parameters", httpHeaders);
        ResponseEntity<String> x = restTemplate.exchange("https://www.anapioficeandfire.com/api/characters", HttpMethod.GET, httpEntity, String.class);

        return x.getBody();

    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1