I have got the solution:
import jenkins.*
import jenkins.model.*
import hudson.model.*
import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.auth.*
import org.apache.commons.httpclient.methods.*
def buildNumber = 2
def urlDeployment = "http://<servername>:8080/hudson/job/"
def buildVersionURL = urlDeployment +
"selenium2-portalservices-FavoriteGameTests-2.2/api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='dunia'
and action/cause/upstreamBuild=${buildNumber}]/number/text()"
final HttpClient client = new HttpClient();
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, new
UsernamePasswordCredentials("<username>", "<apiToken>"));
URI uri = new URI(buildVersionURL,false)
final GetMethod method = new GetMethod();
method.setURI(uri)
client.executeMethod(method);
response = method.getResponseBodyAsString()
Am Mittwoch, 21. November 2012 13:00:37 UTC+1 schrieb Sven Finsterwalder:
>
> Hello,
>
> i hope some one can help me.
> I have this groovy script:
> import jenkins.*
> import jenkins.model.*
> import hudson.model.*
> import org.apache.commons.httpclient.*
> import org.apache.commons.httpclient.auth.*
> import org.apache.commons.httpclient.methods.*
>
> def username = "name"
> def apiToken = "token"
> def client = new HttpClient()
> client.getState().setCredentials(new AuthScope("
> http://hudson/login?from=%2Fhudson%2F",8080,"realm"),new
> UsernamePasswordCredentials(username, apiToken))
> client.params.authenticationPreemptive=true
>
> def buildNumber = 2
> println("hello${buildNumber}")
>
> def urlDeployment = "
> http://hudson/job/selenium2-portalservices-FavoriteGameTests-2.2/"
> def urlPart =
> "api/xml?depth=1&xpath=/freeStyleProject/build[action/cause/upstreamProject='dunia'
>
> and action/cause/upstreamBuild=${buildNumber}]/number/text()"
> println(urlDeployment +""+urlPart)
>
> def get = new GetMethod()
> get.setURI(new URI(urlDeployment+""+urlPart,false))
> println("here")
> get.setDoAuthentication(true)
> get.doAuthentication=true
> println("there "+ get.getDoAuthentication())
> println("wow")
> response = get.getResponseBodyAsString()
> println(response)
>
> But i do not get the correct answer. I got always:
>
> Authentication required
>
>
> What is wrong with my script, please help me.
>
>