title: Java POST request to get token from web service
body: <p>I need to get access from java application to some RESTful web service which uses token-based authentication. As I understood the best choice for this purpose is to use JAX-RS-based libraries like Jersey, but I am very new to this matter. Maybe someone could help me by giving example code of proper request to get a token from web service.</p>
  
  <p>What we have:</p>
  
  <ul>
  <li>URI of the token issuing server. It uses oAuth2 authorization. </li>
  <li>clientId and clientSecret. We have to submit them to the token issuing server which will verify them and return a token.</li>
  <li>URI of the web service itself.</li>
  <li>username and password for service access.</li>
  </ul>
  
  <p>As I understood, to get a token I have to send POST request along with the following headers:</p>
  
  <ul>
  <li>"Authorization", "Basic
  YWRhMGI3NTicdscsN2I0MjNjM2EwNWQ0MjM2ZTg6QU1hS0ltUEZJaUFSR3dGMmJ3NjZZVi9Ec05YZTd0ZkEerfrvegezNoND0=" ("Basic " + base64 encoded "clientId:clientSecret")</li>
  <li>"Accept", "application/x-www-form-urlencoded"</li>
  <li>"Content-Type", "application/json;odata=verbose"</li>
  </ul>
  
  <p>and the following parameter:</p>
  
  <p>grant_type=password&amp;username=someusername&amp;password=somepassword&amp;scope=profile</p>
  
  <p>Hope somebody will help me with example code.</p>