I'm writing a short program to collect metrics from projects on SonarQube.
I am utilising two of SonarQube's apis: api/components/search and api/measures/component.
I am authenticating with use of a token, and have done this with no trouble with the regular curl command for both.
In my Java program, the component api is authenticating fine and returns results. However, the search api (when authenticating with exactly the same method) just returns:
{"errors":[{"msg":"Authentication is required"}]}
Any ideas on why the two apis are exhibiting different behaviour when called in the Java program?
curl commands executed (works for both):
curl -u TOKEN: https://sonarqube-XXX.co.uk/api/components/search?qualifiers=DIR&q=master
curl -u TOKEN: https://sonarqube-XXX.co.uk/api/measures/component?componentId=XXX&metricKeys=coverage
Java code for authentication & HTTP request with search api (works for the component api):
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(TOKEN, "");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient httpClient = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpGet httpGet = new HttpGet("https://sonarqube-XXX.co.uk/api/components/search?qualifiers=DIR&q=master");
HttpResponse response = httpClient.execute(httpGet);
String componentInfo = EntityUtils.toString(response.getEntity());