Search Projects Web API returns empty

Hi,

I’m trying to get the projects of my organization using the search_projects web api with simple java HttpUrlConnection class.

The url used is: https://MY_TOKEN@sonarcloud.io/api/components/search_projects?f=analysisDate&s=analysisDate&pageSize=500&organization=MY_ORGANIZATION

If the same url is used with postman the response is Ok and all components were returned, but when the query was made through java code the json returns empty.

Can anyone please help with this?

Here is the java code used:

HttpURLConnection connection = null;
String body = null;

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod(“GET”);

log.debug(“URL [{}]”, url);

body = new String(IOUtils.toString(connection.getInputStream(), Charset.defaultCharset()));

log.debug(“HttpStatus [{}] - Return [{}]”, connection.getResponseCode(), body);

Hi Tássio,

Welcome to the SonarSource Community!

You code samples are not complete and you may not be using your token to set credentials. These should be added in an Authorization header.

Can you try this and let me know if this resolves your issue? If it does not, can you post the full Java code (omitting and sensitive details)?

Cheers.

Brian

@bcipollone Thanks for your answer,

I tried to use the token on Authorization using basic as below but got a 401 answer.

connection.setRequestProperty(“Authorization”, "Basic " + server.getToken() + “:”);

Now the complete code is like this, the different logics for the host is because I’m extracting data from non sonarcloud servers:

public String connectionFindAllProjects(UserData server) throws IOException {
log.info(“Conectando ao Sonar [{}] para Buscar todos os Projetos”, server.getHost());

this.url = new URL(ApisHelper.getFindAllProjectsUrl(server));

log.info("....... Conectado e Retornado resposta.");

return connectionCore(this.url, server);

}

public static String getFindAllProjectsUrl(final UserData server){
StringBuilder builder = new StringBuilder();

builder.append(server.getCompletHost());
builder.append("/api/components/search_projects?f=analysisDate&s=analysisDate&pageSize=500");
if(server.getOrganization() != null && !server.getOrganization().isEmpty()){
	builder.append("&organization=");
	builder.append(server.getOrganization());
}
	
return builder.toString();

}

public String getCompletHost(){
StringBuilder sb = new StringBuilder();

if (this.token != null && !this.token.isEmpty()){
	sb.append("https://");
	sb.append(token);
	sb.append("@");
} else {
	sb.append("http://");
}
	
sb.append(this.host);

if (this.port != null && !this.port.isEmpty()){
	sb.append(":");
	sb.append(this.port);
}
	
if (this.context != null && !this.context.isEmpty()){
	sb.append("/");
	sb.append(this.context);
}
	
return sb.toString();

}

private String connectionCore(final URL url, final UserData server) throws IOException {
HttpURLConnection connection = null;
String body = null;
String encoding = null;

encoding = Base64.getEncoder()
		.encodeToString((server.getUser() + ":" + server.getPassword()).getBytes("UTF-8"));

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

    // For sonarcloud servers the user token is added on url
if (server.getToken() == null || server.getToken().isEmpty()){
	connection.setRequestProperty("Authorization", "Basic " + encoding);
} 

log.debug("URL [{}]", url);
	
body = new String(IOUtils.toString(connection.getInputStream(), Charset.defaultCharset()));
	
log.debug("HttpStatus [{}] - Return [{}]", connection.getResponseCode(), body);
	
if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK){
	return null;
}

return body;

}

Thanks!

Hi,

The problem were ocurring because when used token I simply was setting the value on Authorization header, the solution is below just needed to encode properly:

        String token = Base64.getEncoder().encodeToString((server.getToken() + ":").getBytes("UTF-8"));
        connection.setRequestProperty("Authorization", "Basic " + token);

Tássio,

Glad to hear this is working and thank you for updating the Community.

Brian

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.