title: Spring 4 MVC mechanism to connect to OAuth2 Provider's URL via HTTP Post
body: <p>Am using Spring 4 MVC to create RESTful Web Services.</p>
  
  <p>Need to use an HTTP Post to obtain an access token (by passing in an id and secret given from an OAuth provider):</p>
  
  <p>curl command:</p>
  
  <pre><code>curl -X POST \
  https://api.app.com/v1/oauth/token \
  -H 'content-type: application/x-www-form-urlencoded' \
  -d 'cliend_id=12354aaa&amp;secret=423aaBCd’
  </code></pre>
  
  <p>MyController:</p>
  
  <pre><code>@RestController
  public class MyController {
  
      private static String url = "https://api.app.com/v1/oauth/token";
      private HttpHeaders headers = null;
  
      @Autowired
      public MyController() {
          headers = new HttpHeaders();
          headers.add("Content-Type", "application/x-www-form-urlencoded");
      }
  
      @RequestMapping(value = "/getAccessToken", method = RequestMethod.POST, produces="application/json")
      public AccessToken getAccessToken(@RequestParam(value="client_id", required=true) String client_id, @RequestParam(value="secret", required=true) String secret) {
          AccessToken token = // How to get token from the response? 
          return token;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Tried using this:</p>
  
  <pre><code>ResourceOwnerPasswordResourceDetails resourceDetails = new ResourceOwnerPasswordResourceDetails();
  resourceDetails.setAccessTokenUri("https://api.app.com/v1/oauth/token");
  resourceDetails.setClientId("12354aaa");
  resourceDetails.setClientSecret("423aaBCd");
  resourceDetails.setGrantType("client_credentials");
  resourceDetails.setScope(OAuthUtils.getScopesList("read", "write"));
  
  DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
  
  oauth2RestTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
  
  OAuth2AccessToken token = oauth2RestTemplate.getAccessToken();
  System.out.println(token);
  </code></pre>
  
  <p>This returns:</p>
  
  <pre><code>Exception in thread "main" error="access_denied", error_description="Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it."
      at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:153)
      at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:121)
      at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
      at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
  </code></pre>
  
  <p>(1) How can I get the accessToken from the OAuth Provider's URL from inside MyController's method?</p>
  
  <p>(2) Is there a better way (Spring library or mechanism) which I can obtain the access token for each subsequent call (for example HTTP GET) like an automatic mechanism? </p>
  
  <p>(3) Should I be using something else like RestTemplate to make these calls?</p>