title: How to access external URL which requires OAuth2 via Spring Boot?
body: <p>Currently, the mechanism that we use for authenticating against a server which requires OAuth2 is to write a Java program which contains a main() method, which runs an HttpClient to generate an OAuth2 access token by using this call:</p>
  
  <pre><code>https://api.externalsite.com/v1/oauth/token?clientId=iLHuXeULFBdW4B1dmRY0MhFILRQnlfeK&amp;clientSecret=RG3JanXEq2R1GhRvIQ2d2AKRx0SORvb3&amp;grant_type=client_credentials
  </code></pre>
  
  <p>This returns the following JSON payload:</p>
  
  <pre><code>{
      "access_token": "eyJhbGciOi786I1NiJ9.eyJ1c2VybmFtZSI6bnVsbCwiZGV2aWNlSWQiOm51bGwsImNsaWVudElkIjoiaUxIdVhlVUxGQmRXNEIxZG1SWTBNaFJPTVJRbmxmZUsiLCJhZElkIjpudWxsLCJleHAiOjE1MjU0MjY4LMYsImlhdCI6MTUyNTQyMzE0Nn0.Zz_uhXqOF2ykC24mNBWHnQ_Vmx-jfQs3X4qcmmN0-Sk",
      "token_type": "Bearer",
      "expires_in": 3600,
      "refresh_token": null,
      "scope": null
  }
  </code></pre>
  
  <p>After obtaining the access token, we are able to run queries using JSON against the authorized website / service.</p>
  
  <hr>
  
  <p>Question(s):</p>
  
  <ol>
  <li><p>Inside a Spring Boot Microservice (2.0.1.RELEASE), how can one use Spring Security or just an HttpClient to use clientId, clientSecret and grant_type to automatically provide a global access token inside each REST call (which might be an HTTP Post) from the REST controller layer? </p></li>
  <li><p>Can someone show a code sample of how to use Spring Security or a different library to just send the clientId, clientSecret, and grant_type to obtain an OAuth2 access token?</p></li>
  <li><p>What to do (using the library from question # 2) if the OAuth2 token expires?</p></li>
  </ol>