Basic OAuth Call RESTAPI in JMIX

Hi,

this is a very basic question, but I am not getting through in posting a successful oauth call for a Jmix app.

The code I am using is as follows:

public class SimpleCall {

public static void main(String[] args) throws Exception{
    String url = "http://localhost:8080/oauth/token";
    DefaultHttpClient http = new DefaultHttpClient();
    
    
    CredentialsProvider cp = new BasicCredentialsProvider();
    cp.setCredentials(new AuthScope("localhost", 8080), 
            new UsernamePasswordCredentials("client", "secret"));
    http.setCredentialsProvider(cp);
    
    HttpPost postRequest = new HttpPost(url);
    postRequest.addHeader("Content-Type","application/x-www-form-urlencoded");
    //postRequest.addHeader("Authorization", "client secret");
    
    postRequest.getParams().setParameter("grant_type", "password").setParameter("username", "admin").setParameter("password", "admin");
    
    HttpResponse response = http.execute(postRequest);
    if (response.getStatusLine().getStatusCode() != 200) {
		throw new RuntimeException("Failed : HTTP error code : "
		   + response.getStatusLine().getStatusCode());
	}

    BufferedReader br = new BufferedReader(
             new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
            System.out.println(output);
    }

    http.getConnectionManager().shutdown();

}

Does “client/secret” need to be something different? In CUBA you used to have the parameter “Basic Y2xpZW50OnNlY3JldA==”, so here client/secret looks pretty “open”, however via cURL following the instructions in Getting Started with REST :: Jmix Documentation I managed to retrieve auth_token’s.

No idea why I keep getting 400/401 http errors when launching these calls. I would appreciate if someone could post a successfull call to a Jmix app or share any insights on how to put this in the right path.

Many thanks in advance.
Regards,
Carlos.

Got it working entering the params in the url like this:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.*;
import org.apache.http.entity.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.*;

/**
 *
 * @author Carlos Conti
 */
public class SimpleCall {
    
    public static void main(String[] args) throws Exception{
        String url = "http://localhost:8080/oauth/token?grant_type=password&username=admin&password=admin";
        DefaultHttpClient http = new DefaultHttpClient();
        
        
        CredentialsProvider cp = new BasicCredentialsProvider();
        cp.setCredentials(new AuthScope("localhost", 8080), 
                new UsernamePasswordCredentials("client","secret"));
        http.setCredentialsProvider(cp);
        
        HttpPost postRequest = new HttpPost(url);
        postRequest.addHeader("Content-Type","application/x-www-form-urlencoded");
        //postRequest.addHeader("Authorization", "client secret");
        
        //postRequest.getParams().setParameter("grant_type", "password").setParameter("username", "admin").setParameter("password", "admin");
        
        HttpResponse response = http.execute(postRequest);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
               + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(
                 new InputStreamReader((response.getEntity().getContent())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
                System.out.println(output);
        }

        http.getConnectionManager().shutdown();

    }
    
}

Quite annoying. Hope it helps anyone.

Regards,
Carlos.