小编Ste*_*anS的帖子

在 Spring RestTemplate 中设置 Authorization 标头

我试图在 Spring 的 RestTemplate 的帮助下访问 RestAPI-Endpoint

public List<Transaction> getTransactions() {
    // only a 24h token for the sandbox, so not security critical
    // still I replaced the last 10 digits here with 'x' but not in my original code
    String authToken = "tylhtvGM6Duy8q0ZBbGaTg2FZefLfyeEeMZvCXlU2bEiinnZcLSACTxxxxxxxxxx";
    String encodedAuthToken = Base64.getEncoder().encodeToString(authToken.getBytes());

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    headers.add("Authorization", "Bearer "+encodedAuthToken );

    ResponseEntity<TransactionsResponse> response = restTemplate.exchange(
            "https://api-sandbox.starlingbank.com/api/v1/transactions",
            HttpMethod.GET,
            new HttpEntity<>("parameters", headers),
            TransactionsResponse.class
    );

    return response.getBody().getEmbedded().getTransactions();
}
Run Code Online (Sandbox Code Playgroud)

but I get a HttpClientErrorException saying "403 Forbidden". Long …

spring authorization resttemplate

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

authorization ×1

resttemplate ×1

spring ×1