Skip to Main Content

Java APIs


For appeals, questions and feedback, please email

HttpURLConnection (grant_type=client_credentials) - Server returned response code 400

Need your help. Thank you.

 public String getAuthToken() throws SQLException{
    HttpURLConnection httpConnection =null;
    URL url = new URL(null, tokenUrl);
    httpConnection = (HttpURLConnection) url.openConnection();
    String authString = clientId + ":" + clientSecret;

    //-- header
    httpConnection.setRequestProperty("charset", "utf-8");
    httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpConnection.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(authString.getBytes()));

    //-- body
    Map<String,String> mp = new HashMap<String,String>();
    mp.put("grant_type", "client_credentials");
    mp.put("client_id", clientId);
    mp.put("client_secret", clientSecret);
    mp.put("scope", scope);

    byte[] requestBody = buildData(mp).getBytes( StandardCharsets.UTF_8 );
    try(DataOutputStream out = new DataOutputStream(httpConnection.getOutputStream())) {
      out.write( requestBody );

    if (httpConnection.getResponseCode() == 200 ){
     // success
    } else {
     // fail

The result of httpConnection.getResponseCode() is 400.
The result of httpConnection.getErrorSteam() is:

 "error_description":"The request contains invalid parameters or values."} 

Any idea how to find out which parameter is invalid or which parameter has invalid value?
In addition, the code works if replace

    mp.put("grant_type", "client_credentials");
    mp.put("client_id", clientId);
    mp.put("client_secret", clientSecret);


    mp.put("grant_type", "password");
    mp.put("username", userName);
    mp.put("password", passWord);

Thank you so much for your help.

This post has been answered by User12617166-Oracle on Sep 28 2022
Jump to Answer
Post Details
Added on Sep 20 2022
1 comment