1

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());
L. Ball
  • 21
  • 4
  • Show us the command you are executing. See also [the documentation](https://docs.sonarqube.org/display/DEV/Web+API), the "User Token" paragraph. – Jeroen Heier Mar 02 '19 at 20:23
  • @JeroenHeier added – L. Ball Mar 02 '19 at 21:00
  • You should have a look [here](https://stackoverflow.com/questions/7929013/making-a-curl-call-in-c-sharp) (BenW answer) and [here](https://stackoverflow.com/questions/46538316/sonarqube-how-to-login-with-the-web-api) – Jeroen Heier Mar 03 '19 at 08:12
  • @JeroenHeier thanks. I have it working fine for the components API, but the search api still shows 'authentication required' for the various methods used in those other threads (which work for components API) – L. Ball Mar 03 '19 at 10:46

0 Answers0