title: Authentication requests using HTTPS to an API in Java
body: <p>I am looking to use an API called <a href="https://www.hooktheory.com/api/trends/docs" rel="nofollow">HookTheory</a>. I am making the HTTPS calls in Java, and since its my first time using HTTP or HTTPS clients I am running into a few road blocks.</p>
  
  <p><a href="https://www.hooktheory.com/api/trends/docs#authentication" rel="nofollow">Here are the document's details on User Authentication:</a></p>
  
  <pre><code>You authenticate to the Hooktheory API by providing an HTTP Bearer Token, according to the OAuth 2 protocol. Your HTTP Bearer Token is retrieved through the API with your www.hooktheory.com username and password. To retrieve your HTTP Bearer Token, make the following request:
  
  POST users/auth
  
  The body of the request must contain your www.hooktheory.com username and password:
  
  {  
        "username": "Hooktheory",
        "password": "0123456789"
  }
  
  The response will contain three fields, as shown below:
  
  {  
        "id": 1234,
        "username": "Hooktheory",
        "activkey": "aoa6jjacz34kcta3aomeqwuz89"
  }
  
  The "activkey" property contains your HTTP Bearer Token; include it as an authorization header in all future requests to the API.
  </code></pre>
  
  <p>Can someone explain in better detail how I would go about doing this in Java?
  My starter code is the following:</p>
  
  <pre><code>String url = "https://api.hooktheory.com/v1/users/auth";
      URL obj = new URL(url);
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  
      //add request header
      con.setRequestMethod("POST");
      con.setRequestProperty("username", username);
      con.setRequestProperty("password", password);
  
      String urlParameters = "sn=C02G8416DRJM&amp;cn=&amp;locale=&amp;caller=&amp;num=12345";
  
      // Send post request
      con.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
      wr.writeBytes(urlParameters);
      wr.flush();
      wr.close();
  
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + urlParameters);
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(
              new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      //print result
      System.out.println(response.toString());
  </code></pre>
  
  <p>This code is basically a <a href="http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/" rel="nofollow">copy of the code here</a>.</p>
  
  <p>I get a 401 error (authentication) when I run the <strong>post</strong> code above. I'm guessing its because I haven't formatted the get/post requests correctly.</p>
  
  <p>Can somebody point me in the right direction?</p>