title: Spring Rest Template with OAUTH
body: <p>My controller layer is wrapped with spring oauth2. I am writing an integration test to test the api calls to controller, so I decided to use <code>RestTemplate</code>. </p>
  
  <p>Following are commands I use via curl:</p>
  
  <pre><code>curl -v --cookie cookies.txt --cookie-jar cookies.txt  "http://localhost:8080/oauth/token?client_id=my-trusted-client&amp;grant_type=password&amp;scope=trust&amp;username=xxxx&amp;password=xxxxxx"
  </code></pre>
  
  <p>This returns an access token which I use to make the call to the api :</p>
  
  <pre><code>curl -v -H "Authorization: Bearer Access toekn value" "http://localhost:8080/profile/1.json"
  </code></pre>
  
  <p>While using <code>RestTemplate</code>, I was able to get the access token, but now I want to pass this token to make api calls:</p>
  
  <pre><code>DefaultHttpClient client = new DefaultHttpClient();
      HttpHeaders headers = new HttpHeaders();
      headers.set("Authorization: Bearer", accessToken);
      System.out.println(accessToken);
      HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(headers);
      System.out.println(restTemplate.exchange("http://localhost:8080/xxxx",HttpMethod.GET,entity,Object.class));
  </code></pre>
  
  <p>However, I get this error:</p>
  
  <pre><code>Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
  at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:75)
  at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
  at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
  at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
  at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:377)
  at com.gogii.service.SKUService.testGetAllSKU(SKUService.java:20)
  </code></pre>
  
  <p>How can we make authenticated calls using <code>RestTemplate</code>? </p>