I’m trying to make an sonarqube rest api call from jenkins pipeline script. However it is failing with 401 (Unauthorized) error if I use groovy URL(url).openConnection() module, but it works with curl.
Hi,
is your sonarAuthToken base64 encoded, and does it end with the separator : ?
Here is my groovy snippet for such cases and it works fine:
import groovy.json.*
def sonarRest(url,method) {
jsonSlurper = new JsonSlurper()
raw = 'your u s e r t o k e n:'
bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(raw.getBytes())
conn = new URL(url).openConnection() as HttpURLConnection
conn.setRequestMethod(method)
conn.setRequestProperty("Authorization", bauth)
conn.connect()
httpstatus = conn.responseCode
object = jsonSlurper.parse(conn.content)
}
I am also trying to run an API ( to find whether a project is exist) using CURL within withSonarQubeEnv but getting 403.
Note: sonarAuthToken is coming as ***** ( masked ) . I suspect the masked token is causing an issue here.
403 means forbidden, so i guess your user has no admin rights.
Have a look on https://yoursonarhost/web_api/api/ and check whether api docs has Requires 'System Administrator' permission or similar in your case.
Thanks Gilbert for your time. yes aware of the admin permissions. If I try with Basic Auth using username:password then i am getting response as follows