如何通过证书从groovy脚本登录到Jenkins?

时间:2018-08-03 13:23:31

标签: json authentication jenkins groovy

我正在尝试使用groovy脚本访问Jenkins作业的日志之一。但出现403错误。如何通过凭据登录以下代码?

def jsonStr1 = new URL(myEnvUrl+"warnings40Result/api/json?pretty=true").getText()

2 个答案:

答案 0 :(得分:0)

您将获得HTTP 403,代表未经授权的尝试。

可能存在Jenkins的登录页面,您应该包括该登录页面才能访问下一页。页。检查以下链接:

Groovy built-in REST/HTTP client?

答案 1 :(得分:0)

def jsonStr1 = new URL(myEnvUrl+"warnings40Result/api/json?pretty=true").getText()

I tried all the solution of url:
https://stackoverflow.com/questions/25692515/groovy-built-in-rest-http-client

i think without Login Credentials code we can't access 'jsonStr1'. so i tried below code, now i am able to access but while parsing the value its giving error: 

code:200
[PostBuildScript] - Problem occurred: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. 


my code:
def  warningJsonUrl = EnvBuildUrl+"warnings40Result/api/json?token=4eca462899e426937a94006a20561011"

def authString = "admin1:admin1".getBytes().encodeBase64().toString()

def conn = warningJsonUrl.toURL().openConnection()
conn.setRequestProperty( "Authorization", "Basic ${authString}" )
if( conn.responseCode == 200 ) {
println("code:"+conn.responseCode)

def textJsonObj = new JsonSlurper().parseText(conn.content.text) 
}
how i will parse as text?