1

I am trying to use Sonarqube webservice API. I need to pass some parameters along with username and password to this web service api.

For that purpose I have written this following code -

HttpClient client = new HttpClient();
        String uri = "http://myUrl.sonar.com";
        client.getState().setCredentials(new AuthScope(uri, 8080, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials("username", "password"));
        GetMethod get = new GetMethod("http://myUrl.sonar.com/api/resources?"
                + "resource=110041&format=json&metrics=ncloc,coverage&verbose=true"
                + "&metrics=sqale_index,sqale_debt_ratio,ncloc,sqale_rating");
        get.setDoAuthentication(true);
        try {
            int status = client.executeMethod(get);
            System.out.println(status + "\n" + get.getResponseBodyAsString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            get.releaseConnection();
        }

When I run this code from Eclipse, it returns

401
{"err_code":401,"err_msg":"Unauthorized"}

Any probable reason for this? the user name and passwords are correct.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Srijani Ghosh
  • 3,935
  • 7
  • 37
  • 68
  • Permissions are correct? – CSchulz Jun 02 '16 at 21:18
  • 1
    Have the user the Browse permission on the project 110041 ? – Julien L. - SonarSource Team Jun 03 '16 at 06:44
  • @JulienL.-SonarSourceTeam , I have tried to browse the project from sonar website, it is working fine. Do I need to check anywhere else? – Srijani Ghosh Jun 06 '16 at 07:15
  • It should be good. Then, are you sure you're using the right login/password ? – Julien L. - SonarSource Team Jun 06 '16 at 07:24
  • @JulienL.-SonarSourceTeam yes.. – Srijani Ghosh Jun 06 '16 at 07:45
  • Can you try to execute the WS query in a browser (on which you are connected with the same login/password) ? – Julien L. - SonarSource Team Jun 06 '16 at 07:48
  • @JulienL.-SonarSourceTeam . Tried. I have logged in via browser and then in another tab I tried to execute the command : http://sonar.wincor-nixdorf.com/api/resources?resource=110041&format=json&metrics=ncloc,coverage&verbose=true&metrics=sqale_index,sqale_debt_ratio,ncloc,sqale_rating. And, it is working fine and it is fetching all the results. – Srijani Ghosh Jun 06 '16 at 07:50
  • So it's probably the Java code that has something wrong – Julien L. - SonarSource Team Jun 06 '16 at 08:48
  • We have an API in order to call WS. For instance you could do : WsClient client = WsClientFactories.getDefault().newClient(HttpConnector.newBuilder() .url(server.getUrl()) .credentials(LOGIN, PASSWORD) .build()); client.wsConnector().call(new GetRequest("http://sonar.wincor-nixdorf.com/api/resources?resource=110041&format=json&metrics=ncloc,coverage&verbose=true&metrics=sqale_index,sqale_debt_ratio,ncloc,sqale_rating")) – Julien L. - SonarSource Team Jun 06 '16 at 08:51
  • Thanks @JulienL.-SonarSourceTeam , can you please provide some more information on this? may be some URL or documentation ? Thanks a lot! – Srijani Ghosh Jun 06 '16 at 09:20
  • The doc is still under construction, but you can have a look in our IT : https://github.com/SonarSource/sonarqube/blob/master/it/it-tests/src/test/java/util/ItUtils.java#L80 and https://github.com/SonarSource/sonarqube/blob/master/it/it-tests/src/test/java/it/user/BaseIdentityProviderTest.java#L272 for instance. Librairie is available in sonar-ws http://search.maven.org/#artifactdetails%7Corg.sonarsource.sonarqube%7Csonar-ws%7C5.6%7Cjar – Julien L. - SonarSource Team Jun 06 '16 at 09:56
  • Thanks @JulienL.-SonarSourceTeam . Surely will have a look at it. – Srijani Ghosh Jun 06 '16 at 10:05
  • @JulienL.-SonarSourceTeam Please add your comment as answer. – CSchulz Jun 06 '16 at 12:13

1 Answers1

0

You should try our API to query some URL, see some examples in our ITs to use it :

You need to use sonar-ws, last version is available here : http://search.maven.org/#artifactdetails%7Corg.sonarsource.sonarqube%7Csonar-ws%7C5.6%7Cjar