spring - 在 Spring RestTemplate 中设置 Authorization header

标签 spring authorization resttemplate

我试图在 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();
}

但我收到一个 HttpClientErrorException 说“403 Forbidden”。
长版
Caused by: org.springframework.web.client.HttpClientErrorException: 403 Forbidden
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]

我的代码基于 former stackoverflow thread并且通过 Postman 使用相同参数的调用成功:
enter image description here

那么问题是什么?

更新

不编码 authToken 没有区别
headers.add("Authorization", "Bearer tylhtvGM6Duy8q0ZBbGaTg2FZefLfyeEeMZvCXlU2bEiinnZcLSACTxxxxxxxxxx");

仍然导致相同的 HttpClientErrorException: 403 Forbidden

更新 2

我回答了我的问题! (简短版本:需要 UserAgent。anwser 中的最终代码)

最佳答案

这个特定的服务器需要一个 UserAgent!值可以是任何值,但它必须存在!

所以最终版本是:

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";

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));        
    headers.add("User-Agent", "Spring's RestTemplate" );  // value can be whatever
    headers.add("Authorization", "Bearer "+authToken );

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

关于spring - 在 Spring RestTemplate 中设置 Authorization header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52784834/

相关文章:

java - 在 wicket 中,结合 wicket :link with IAuthorizationStrategy

java - Google 登录中的致命异常(Fantom 错误)

java - 带有标题 : webservice can't find my header parameters 的 Spring RestTemplate postForObject

Java-调用.net wcf web服务Spring

spring - 使用 Spring boot 在单个浏览器 session 中使用多个 OAuth2 客户端

java - 所有枚举的 Spring 自定义转换器

spring - 在 tomcat 上运行 spring 3 应用程序的问题

Java bug - 无法将授权添加到 HttpURLConnection set/addRequestProperty()

spring - 如何通过 rest 模板使用表单数据发出 http 请求?

spring-boot - Kotlin的一般性问题