title: Setting Authorization header in Spring RestTemplate
body: <p>I'm trying to to access a RestAPI-Endpoint with the help of Spring's RestTemplate</p>
  
  <pre><code>public List&lt;Transaction&gt; 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&lt;TransactionsResponse&gt; response = restTemplate.exchange(
              "https://api-sandbox.starlingbank.com/api/v1/transactions",
              HttpMethod.GET,
              new HttpEntity&lt;&gt;("parameters", headers),
              TransactionsResponse.class
      );
  
      return response.getBody().getEmbedded().getTransactions();
  }
  </code></pre>
  
  <p>but I get a HttpClientErrorException saying "403 Forbidden".
  Long version</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>My code is based on a <a href="https://stackoverflow.com/a/43590414/4515050">former stackoverflow thread</a> and a call with the same parameters via Postman is successful:
  <a href="https://i.stack.imgur.com/ete9V.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ete9V.jpg" alt="enter image description here"></a></p>
  
  <p>So what is the problem?</p>
  
  <p><strong>Update</strong></p>
  
  <p>not encoding the authToken makes no difference</p>
  
  <pre><code>headers.add("Authorization", "Bearer tylhtvGM6Duy8q0ZBbGaTg2FZefLfyeEeMZvCXlU2bEiinnZcLSACTxxxxxxxxxx");
  </code></pre>
  
  <p>still leads to the same <em>HttpClientErrorException: 403 Forbidden</em></p>
  
  <p><strong>Update2</strong></p>
  
  <p>I answered my question! (short version: UserAgent required. final code in anwser)</p>