Q: I am unable to call the SonarQube API from Java to search for issues get 410 error Hi there I am writing an OpenSource Springboot microservice to get issues from SonarQube
I use SonarQube is installed in a Docker container
From the browser I call successfully after logging in :
http://localhost:9001/api/issues/search?componentKeys=za.co.nico:RabbitMqPoc
I am unit testing a Java class that I can call the same URL and getting a 401
failing Authentication where I need it to work
package za.co.nico.poc.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.WsResponse;
import org.springframework.stereotype.Service;

/**
 * 
 * https://www.baeldung.com/java-http-request
 * 
 */
@Service
public class SonarServiceImpl implements SonarService {
    private static final Logger log = LoggerFactory.getLogger(SonarServiceImpl.class);
    
    private final String TOKEN="2ce06b3585c34141beeeb4005235337ba2bd135d"; 
    
    /**
     * https://programtalk.com/java-api-usage-examples/org.sonarqube.ws.client.WsClient/
     */
    @Override
    public String getData(String restUrl,String sonarEndPoint) {

        log.debug(" restUrl : "+restUrl+" sonarEndPoint : "+sonarEndPoint); // restUrl : http://localhost:9001/ sonarEndPoint : api/components/search_projects

        String login="admin";
        String password="admin";
        
        WsClient wsClient = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder().url(restUrl).credentials(login, password).build());
        WsResponse response = wsClient.wsConnector().call(new GetRequest("api/authentication/validate"));
        String content = response.content();
        log.debug(""+response.isSuccessful());
        log.debug(""+response.code()); //200

        response = wsClient.wsConnector().call(new GetRequest("api/components/search_projects"));
        log.debug(""+response.isSuccessful());
        content = response.content();
        log.debug(""+response.code()); //401
        
        
        wsClient = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder().url(restUrl).credentials("admin", "admin").build());
        response = wsClient.wsConnector().call(new GetRequest("local_ws_call/require_permission"));
        log.debug(""+response.isSuccessful());
        log.debug(""+response.code()); // 200

        wsClient = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder().url(restUrl).credentials(login, password).build());
        response = wsClient.wsConnector().call(new GetRequest("api/rules/search"));
        log.debug(""+response.isSuccessful());
        log.debug(""+response.code()); // 401
        
        return "";
    }

}

Please advise how to fix this

A: A 401 error simply means that the authentication credentials you are providing did not match what was expected.  It appears you are providing a username and password of "admin" and "admin", but I also see that you defined a "TOKEN" constant, which is unreferenced.  We would have no idea how you configured your sonarqube instance, or what credentials are actually required.  In my experience, it's best for automation scripts to use the "token" authentication system, which you appear to have been intending to use, but you're simply not passing it correctly.
