【发布时间】:2016-10-01 20:52:55
【问题描述】:
我正在尝试使用 Sonarqube 网络服务 API。我需要将一些参数连同用户名和密码一起传递给这个 Web 服务 api。
为此,我编写了以下代码 -
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();
}
当我从 Eclipse 运行这段代码时,它会返回
401
{"err_code":401,"err_msg":"Unauthorized"}
这有什么可能的原因吗?用户名和密码正确。
【问题讨论】:
-
权限是否正确?
-
用户是否拥有项目 110041 的浏览权限?
-
@JulienL.-SonarSourceTeam ,我尝试从声纳网站浏览该项目,它工作正常。我需要检查其他地方吗?
-
应该不错。那么,您确定您使用的是正确的登录名/密码吗?
-
@JulienL.-SonarSourceTeam 是的..
标签: java web-services sonarqube apache-commons-httpclient sonarqube-web