【问题标题】:I am unable to call the SonarQube API from Java to search for issues get 410 error我无法从 Java 调用 SonarQube API 来搜索问题得到 410 错误
【发布时间】:2021-07-12 02:07:33
【问题描述】:

您好,我正在编写一个开源 Springboot 微服务来从 SonarQube 获取问题 我使用 SonarQube 安装在 Docker 容器中 从我登录后成功调用的浏览器: http://localhost:9001/api/issues/search?componentKeys=za.co.nico:RabbitMqPoc

我正在对一个 Java 类进行单元测试,我可以调用相同的 URL 并获得 401 在我需要它工作的地方验证失败

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 "";
    }

}

请告知如何解决此问题

【问题讨论】:

    标签: java api sonarqube


    【解决方案1】:

    401 错误仅表示您提供的身份验证凭据与预期不符。您似乎提供了“admin”和“admin”的用户名和密码,但我还看到您定义了一个未引用的“TOKEN”常量。我们不知道您如何配置您的 sonarqube 实例,或者实际需要什么凭据。根据我的经验,自动化脚本最好使用您似乎一直打算使用的“令牌”身份验证系统,但您根本没有正确传递它。

    【讨论】:

    • 嗨大卫谢谢你的评论我并使用默认凭据登录到我的 SonarQube 管理员我还在服务器上创建了令牌我不知道如何在这里使用它我真的很沮丧如何Java 程序员很难获得有关 sorarQube APi 的信息
    • 我正在尝试编写一个独立的微服务来调用 SonarQobe API,并且不能使用脚本,因为它必须在 Docker 容器中运行
    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2018-03-04
    • 2014-02-25
    相关资源
    最近更新 更多