当我试着打电话时:
https://sonar.mydomain.com/api/resources?resource=com.mydomain.project:MY&metrics=ncloc&format=json
我明白了
{"err_code":401,"err_msg":"Unauthorized"}
Run Code Online (Sandbox Code Playgroud)
我如何通过我的凭证?
cod*_*zjx 16
根据最新文档说:SonarQube现在支持双向身份验证:
这是推荐的方式.令牌通过HTTP基本身份验证的登录字段发送,这种方式会更安全,没有任何密码.有关如何生成令牌的更多信息,请访问此页面用户令牌.使用curl发送请求,如下所示:
curl -u THIS_IS_MY_TOKEN: https://sonarqube.com/api/user_tokens/search
# note that the colon after the token is required in curl to set an empty password
Run Code Online (Sandbox Code Playgroud)
登录名和密码通过标准HTTP Basic字段发送:
curl -u MY_LOGIN:MY_PASSWORD https://sonarqube.com/api/user_tokens/search
Run Code Online (Sandbox Code Playgroud)
根据文档, SonarQube使用基本身份验证.尝试:
curl -u admin:SuPeRsEcReT "https://sonar.mydomain.com/api/resources?resource=com.mydomain.project:MY&metrics=ncloc&format=json"
Run Code Online (Sandbox Code Playgroud)
显然,传递这些凭据的机制取决于您调用API的方式.
这也可以在Web浏览器中使用.尝试登录Webui,您的浏览器通常会缓存凭据.
发生这种情况是因为您的 api 调用中不包含身份验证数据。我就是这样解决的。
1.首先在浏览器中安装一个rest客户端“Postman”。
2.打开它并将您的API调用网址放在“正常”选项卡下。
3.转到“基本身份验证”选项卡并输入用户名、密码,然后单击刷新标题。
4.返回“正常”选项卡。您将在标题列表中看到一个名为“Authorization”的标题。
5.现在点击“发送”按钮查看结果。
6.如果您使用的是第 3 方应用程序,请使用邮递员生成的值将“授权”标题添加到您的呼叫中。