AnsweredAssumed Answered

"Invalid Login" error when trying to authenticate via Java and the REST API

Question asked by SpockPT SpockPT on Mar 5, 2014
Latest reply on Aug 31, 2016 by Rishu Gupta
Hi there...

I tryed to do a Java Rest Client to communicate with SugarCRM... I have done this many times in PHP but not Java...
I know that i pass as argument the right credentials but it always return me: {"name":"Invalid Login","number":10,"description":"Login attempt failed please check the username and password"}

Can someone try to understand what is wrong?

Thank you in advance,


private String login(String username, String password) throws NoSuchAlgorithmException, ClientProtocolException, IOException {
        try {

            /*   MessageDigest md5 = MessageDigest.getInstance("MD5");
             String passwordHash = new BigInteger(1, md5.digest(password.getBytes())).toString(16);
            // the order is important, so use a ordered map
            Map<String, String> userCredentials = new LinkedHashMap<>();
            userCredentials.put("user_name", username);
            userCredentials.put("password", password);
            userCredentials.put("encryption", "PLAIN");

            // the order is important, so use a ordered map
            Map<String, Object> request = new LinkedHashMap<>();
            request.put("user_auth", userCredentials);
            request.put("application_name", "RestClient");

            MultipartEntity multipartEntity = new MultipartEntity();
            multipartEntity.addPart("method", new StringBody("login"));

            // define request encoding
            multipartEntity.addPart("input_type", new StringBody("JSON"));

            // define response encoding
            multipartEntity.addPart("response_type", new StringBody("JSON"));

            multipartEntity.addPart("rest_data", new StringBody(JSONObject.valueToString(request)));
            // yourSugarCRM has to be changed to your SugarCRM instance
            // something like localhost/sugarcrm
            String url = "http://localhost/javalicrm/ce64/service/v4_1/rest.php";
            HttpPost httpPost = new HttpPost(url);

            DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
            HttpResponse execute = defaultHttpClient.execute(httpPost);

            HttpEntity entity = execute.getEntity();

            InputStream parse = entity.getContent();
            return convertStreamToString(parse);
        } catch (JSONException ex) {
            Logger.getLogger(TimerSugar.class.getName()).log(Level.SEVERE, null, ex);
        return "x";