I tried to use the following method which is used in other areas of the app but I still get 401:
The original question is posted here: Java 6 EE Client Credentials Bearer
String urlParameters = "client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=client_credentials";
byte[] postData = urlParameters.getBytes("UTF-8");
int postDataLength = postData.length;
String TokenURL = "URL for getting the token here";
URL url = new URL(TokenURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
wr.flush();
System.out.println("->->-> We will send: " + urlParameters);
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED && conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("->->-> Returned Http Code: " + conn.getResponseCode());
throw new RuntimeException("Failed : HTTP error code for Getting Token XFORMURLENCODED : " +
conn.getResponseCode());
}