'How to fetch token from Token API using Java

I am trying to fetch token by calling token API but unable to fetch the same. It is working in postman. Postman details are as follows Postman details

headers

My code

import java.io.*;
import java.net.*;
public class GetToken {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String accessToken = "";
        String tokenURL="https://test.iapsoftware.com/iap6/MobileServices/token";
        String grantType = "password";
        String userName="APITester";
        String password="password"; 
        String ClientCode ="iaptest1";
        String ClientInterface ="API";
        
                try {
                     URL url = new URL(tokenURL);
                    HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
                    // Set header
                    httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    httpConn.setRequestProperty("ClientCode", ClientCode);
                    httpConn.setRequestProperty("ClientInterface", ClientInterface);
                    
                    //httpConn.setRequestBody();
                    httpConn.setDoOutput(true);
                    httpConn.setDoInput(true);
                    httpConn.setRequestMethod("POST"); 
                    //Set Request Body
                    String jsonInputString =("grant_type="+grantType+",username="+userName+",password="+password);
                            
                    OutputStream os = httpConn.getOutputStream();
                    byte[] input = jsonInputString.getBytes("utf-8");
                    os.write(input, 0, input.length);            
                     // Read the response.
                    InputStreamReader isr=null;
                        if (httpConn.getResponseCode() == 200) {
                     isr = new InputStreamReader(httpConn.getInputStream());
                    }
                            else
                         {
                                               isr = new InputStreamReader(httpConn.getErrorStream());
                   }
                    BufferedReader in = new BufferedReader(isr);
                    String responseString = "";
                    String outputString = "";
                   // Write response to a String.
                    while ((responseString = in.readLine()) != null) {
                     outputString = outputString + responseString;
                    }
                       accessToken = outputString;
                } 
                catch (Exception e) 
                {
                    accessToken = "Error"; //+ e.getMessage();            
                }

                System.out.println(accessToken);
        
    }

}

error error screenshot

-As per my understanding I am not passing correct input. Input needs to be URL encoded and I am sending it as JSON. Kindly suggest and provide solution for the same.



Solution 1:[1]

After setting up the body correctly it worked

String urlParameters  = ("grant_type="+grantType+"&username="+userName+"&password="+password);
                                            
OutputStream os = httpConn.getOutputStream();
                    
byte[] postData = urlParameters.getBytes("utf-8");
int postDataLength = postData.length;
                    
os.write(postData, 0, postDataLength);  
 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saman Salehi