SonarQube 如何使用 Web API 登录 [英] SonarQube How to login with the Web API

查看:59
本文介绍了SonarQube 如何使用 Web API 登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a web application that can display code smell result from the Sonarqube, but I also want it can create custom rules sometimes. Currently, I'm able to get the data from the server using HTTPClient in Java or XMLHttpRequest in Js. However, I'm really stuck on POST message to the server.

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

In Js, I've tried these code to log in: (the CORS has been disabled in my chrome)

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

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

The response status code is 200 which is successful. However, when I try to perform some action that needs permission

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

结果是401,未经授权.

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

After some research, I've changed my code to

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,没有找到方法.

The response is 405, no method found.

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

After further research, someone mentioned the request.withCredentials should set to true. I added in, then I got CORS problem again. (with the CORS disabled)

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

(I'm bit confused about the Sonarqube API. The reason I'm saying this is, in my opinion, the purpose of this API is to simplify the method to play with the Sonarqube server externally. However, since it does not allow CORS, does that mean I cannot use the API on my own web page?)

由于Js没运气,所以转用Java.

Since there is no luck in Js, I switched to Java.

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

In Java, I ran this: (I've done login as well)

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 

然后我按照这个关于使用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 服务器?

In summary, my question is: How can I use Java code or Js code (preferred) to POST messages to the SonarQube server with authorization?

感谢任何帮助!

更新

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

I'm now trying with curl, here is what I run in terminal

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

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

And I got this response, which I don't know how

{"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"}]}

推荐答案

适用于遇到相同问题的任何人.我已经在处理 Java 代码了,这是我的代码

For anyone who got the same issue. I've got it working on Java code, Here is my code

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 方法.(来自我的第二次更新)

You may notice I've added a line which sets the header. I've done something similar before to encode my login and password to this base64 format, but they all not working somehow. This working encoded string was taking from the CURL method. (from my second update)

我尝试了一些在线 base64 编码和解码工具,但结果与我从 CURL 方法中得到的结果不符,因此,如果您对此感到苦恼,请运行 CURL 以获取编码的标头并将其传入!如果有人能解释一个更好的版本,那就太好了!

I've tried some online base64 encoding and decoding tool, but the result doesn't match what I got from the CURL method, So if you are struggling on this, run CURL to get your encoded header and pass it in! If anyone could explain a better version, that would be great!

另外,我仍然对让 Js 版本工作感兴趣.如果你知道答案,请分享!

Also, I'm still interested in get the Js version working. If you know the answer, please share!

这篇关于SonarQube 如何使用 Web API 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆