title: Consume an OAuth-secured REST webservice using Spring oauth2
body: <p>I want to consume a REST webservice from a server which protects his resources using oauth2.</p>
  
  <p>I use Spring boot (JHipster).</p>
  
  <p>To do this i have in <code>SecurityConfiguration</code> class this :</p>
  
  <pre><code>@Value("${oauth.resource:http://sercverUsingOAuth2}")
  private String baseUrl;
  
  @Value("${oauth.authorize:http://sercverUsingOAuth2/rest/oauth/token}")
  private String authorizeUrl;
  
  @Value("${oauth.token:http://sercverUsingOAuth2/rest/oauth/token}")
  private String tokenUrl;
  
  @Bean
  public OAuth2RestOperations oauth2RestTemplate() {
      AccessTokenRequest atr = new DefaultAccessTokenRequest();
      return new OAuth2RestTemplate(resource(),
              new DefaultOAuth2ClientContext(atr));
  }
  
  @Bean
  protected OAuth2ProtectedResourceDetails resource() {
      AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
      resource.setAccessTokenUri(tokenUrl);
      resource.setUserAuthorizationUri(authorizeUrl);
      resource.setClientId("client_id");
      resource.setClientSecret("client_secret");
      resource.setGrantType("grant_type");
      return resource;
  }
  </code></pre>
  
  <p>This class (<code>SecurityConfiguration</code>) is annoted using :</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableOAuth2Client
  </code></pre>
  
  <p>And this is  my <code>controller</code> (Spring MVC) :</p>
  
  <pre><code>@RestController
  @RequestMapping("/consume")
  public class MyContrtoller {
  
  @Inject
  private OAuth2RestOperations oauth2RestTemplate;
  
  @RequestMapping(value = "/oauth2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public List&lt;DataModel&gt; getProducts() {
  
      ResponseEntity&lt;MyModel&gt; forEntity = oauth2RestTemplate
              .getForEntity("http://sercverUsingOAuth2/rest/resourceToConsume",
                      MyModel.class);
      return forEntity.getBody().getData();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>However when i want to consume my webservice (<a href="http://myHost/consume/oauth2" rel="nofollow noreferrer">http://myHost/consume/oauth2</a>) i get this Exception :</p>
  
  <pre><code>    org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException:
   Unable to obtain a new access token for resource 'null'. The provider manager
   is not configured to support it.
  </code></pre>
  
  <p>I have googled and i found this :</p>
  
  <ul>
  <li><a href="https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/client/src/main/java/client/ClientApplication.java" rel="nofollow noreferrer">github</a> </li>
  <li><a href="https://stackoverflow.com/questions/27864295/how-to-use-oauth2resttemplate">stackoverflow</a></li>
  </ul>
  
  <p>But it doesn't help me.</p>
  
  <p>Thanks.</p>