访问私人项目的 SonarQube 质量门 API

Accessing SonarQube quality gate API for private projects

如果是私人项目,我如何访问 SonarQube 质量门 API?我尝试为用户发送 "User token" 作为 Authorization 不记名令牌和基本登录。但是,它没有用。我可以访问它的唯一方法是在用户登录时从浏览器访问,即存在登录令牌。也可以从 Postman 那样访问。我怎样才能获得令牌,这是访问 API 的正确方法吗?

令牌确实是访问 API 的正确方法。

1) 您可以通过以下方式获取新令牌 Administration > Security > Users under tokens(单击列表图标)然后输入名称并单击 Generate

确保复制令牌,因为关闭后您将看不到它。

2) Postman 可以选择根据请求生成代码(单击 "code" 选项)。如果它通过 Postman 工作,它应该可以在您的代码中使用复制的请求。

3) 作为最后的手段,如果它不起作用,这里是我写的一个 Groovy 函数来发送一个 Get 请求。

// Returns response of SonarQube web API call with admin auth in text format
def getFromApi(String getURL){

    def Res = ("curl -u "+AuthenticationToken+": "+getURL).execute().text

    if (Res){
        return Res
    }

    else{ 
        return "error"
    }
}

对于post请求,您只需在身份验证令牌前添加-X POST。修改它以满足您的需要。 希望对您有所帮助!