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

查看:1404
本文介绍了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​​进行基本身份验证的链接

Then I change my code follow this link about 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代码(首选)将消息以授权方式发布到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天全站免登陆