gpt4 book ai didi

java - SonarQube 如何使用 Web API 登录

转载 作者:行者123 更新时间:2023-11-30 02:22:01 24 4
gpt4 key购买 nike

我正在编写一个 Web 应用程序,可以显示 Sonarqube 的代码气味结果,但我也希望它有时可以创建自定义规则。目前,我可以使用Java中的HTTPClient或Js中的XMLHttpRequest从服务器获取数据。但是,我真的被困在向服务器发送消息上。

在Js中,我尝试使用以下代码登录:(CORS已在我的chrome中禁用)

request.open("POST", "http://localhost:9000/api/authentication/login?login=admin&password=admin", true);
request.send();

响应状态码为200,表示成功。但是,当我尝试执行某些需要许可的操作时

request.open("POST", "http://localhost:9000/api/qualityprofiles/copy?fromKey=" + fromKey + "&toName=" + name, true);
request.send();

结果是401,未经授权。

经过一番研究,我将代码更改为

var base64encodedData = ("admin:admin").toString('base64');
request.open("POST", "http://localhost:9000/api/qualityprofiles/copy?fromKey=" + fromKey + "&toName=" + name, true);
request.setRequestHeader("Authorization", "Basic " + base64encodedData);
request.send();

响应为405,未找到方法。

经过进一步研究,有人提到 request.withCredentials 应该设置为 true。我添加进去后,又出现了CORS问题。 (禁用 CORS)

(我对 Sonarqube API 有点困惑。我这么说的原因是,在我看来,这个 API 的目的是简化外部使用 Sonarqube 服务器的方法。但是,因为它确实不允许 CORS,这是否意味着我不能在自己的网页上使用该 API?)

由于Js没有运气,所以我转而使用Java。

在 Java 中,我运行了这个:(我也完成了登录)

HttpPost post = new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);) {

System.out.println(response.getStatusLine());
}

我得到了:

HTTP/1.1 401 

然后我更改我的代码,请点击此链接关于 Basic Authentication with the API

CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials
= new UsernamePasswordCredentials("admin", "admin");
provider.setCredentials(AuthScope.ANY, credentials);

HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();

HttpResponse response = client.execute(
new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt"));
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);

再次,401

总而言之,我的问题是:如何使用Java代码或Js代码(首选)将消息POST到授权的SonarQube服务器?

感谢任何帮助!

更新

我现在正在尝试使用curl,这是我在终端中运行的内容

curl -X POST -u deb3dd4152c571bcdb7b965e1d99b23a4e5c9505: http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=test_file

我收到了这个回复,但我不知道这是怎么回事

{"errors":[{"msg":"The 'toName' parameter is missing"}]}

CURL 的第二次更新

我跑了:

curl -X POST -F "fromKey=AV7dToVK_BWPTtE1c9vU;toName=test_file" -v -u deb3dd4152c571bcdb7b965e1d99b23a4e5c9505: http://localhost:9000/api/qualityprofiles/copy

结果:

*   Trying ::1...
* Connected to localhost (::1) port 9000 (#0)
* Server auth using Basic with user 'deb3dd4152c571bcdb7b965e1d99b23a4e5c9505'
> POST /api/qualityprofiles/copy HTTP/1.1
> Host: localhost:9000
> Authorization: Basic ZGViM2RkNDE1MmM1NzFiY2RiN2I5NjVlMWQ5OWIyM2E0ZTVjOTUwNTo=
> User-Agent: curl/7.49.1
> Accept: */*
> Content-Length: 179
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------c22bb5dd76f44ac4
>
< HTTP/1.1 100
< HTTP/1.1 400
< Content-Type: application/json
< Content-Length: 56
< Date: Wed, 04 Oct 2017 00:43:47 GMT
< Connection: close
<
* Closing connection 0
{"errors":[{"msg":"The 'toName' parameter is missing"}]}

最佳答案

对于遇到同样问题的人。我已经在 J​​ava 代码上运行了,这是我的代码

HttpPost post = new HttpPost("http://localhost:9000/api/qualityprofiles/copy?fromKey=AV7dToVK_BWPTtE1c9vU&toName=testtt");
post.setHeader("Authorization", "Basic ZGViM2RkNDE1MmM1NzFiY2RiN2I5NjVlMWQ5OWIyM2E0ZTVjOTUwNTo=");
try(CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(post);) {

System.out.println(response.getStatusLine());
}

您可能会注意到我添加了一行设置标题。我之前已经做过类似的事情,将我的登录名和密码编码为这种 base64 格式,但它们都无法正常工作。该工作编码字符串取自 CURL 方法。 (来 self 的第二次更新)

我尝试过一些在线base64编码和解码工具,但结果与我从CURL方法得到的结果不匹配,所以如果你在这方面遇到困难,请运行CURL来获取编码后的 header 并将其传入!如果有人能解释一个更好的版本,那就太好了!

此外,我仍然对 Js 版本的运行感兴趣。如果您知道答案,请分享!

关于java - SonarQube 如何使用 Web API 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538316/

24 4 0
文章推荐: java - 为什么在java中加载JNI是在静态初始化程序中完成的?
文章推荐: java - ArrayList 的比较器不能正常工作