Open meishaoyong opened 5 years ago
and i use jenkins-client 0.3.8, and i invoke jenkinsServer#createJob("my-test-job", jobXml) occur error too:
org.apache.http.client.HttpResponseException: Forbidden at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11) at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:345)
Try with crumbFlag true. See: https://wiki.jenkins.io/display/JENKINS/Remote+access+API - CSRF Protection
I have the same problem, when I build a job, sometimes throw exceptions: offbytwo version:0.3.8 jenkins on k8s:2.176.1
@meishaoyong Hey, I have same issue with/without crumbFlag. I am trying to create job by calling createJob() method :-
HttpClientBuilder builder = HttpClientBuilder.create();
JenkinsHttpClient client = new JenkinsHttpClient(uri, builder, "XXX", "XXX");
JenkinsServer jenkins = new JenkinsServer(client);
String sourceXML = readFile("src/main/resources/config.xml");
System.out.println(String.format("Installed Jenkins Version >> %s", jenkins.getVersion().getLiteralVersion()));//works and gives correct result
jenkins.createJob("test-nov1", sourceXML);
or another way -
HttpClientBuilder builder = HttpClientBuilder.create();
JenkinsHttpClient client = new JenkinsHttpClient(uri, addAuthentication(builder, uri, userName, password));
JenkinsServer jenkins = new JenkinsServer(client);
String sourceXML = readFile("src/main/resources/config.xml");
System.out.println(String.format("Installed Jenkins Version >> %s", jenkins.getVersion().getLiteralVersion()));
jenkins.createJob(null,"test-nov1", sourceXML,true);
I am getting same error as of yours like below -
Exception in thread "main" org.apache.http.client.HttpResponseException: status code: 403, reason phrase: Forbidden
at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375)
at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389)
at com.xxx.OffByTwoJenkins.main(OffByTwoJenkins.java:46)
Are you able to call createJob() method without error? If yes, then please post your code also and setting information in Jenkins. P.S: When I changed "Any user can do Any thing" in Jenkins under /configureSecurity, there is no error. What I found is that when without this setting, when we hit Jenkins, Jenkins still uses Anonymous user for log in. And due to this, this error is coming. Please post your thoughts and any solution/workaround if any you found. Thanks
@meishaoyong I have same version as of yours. However, as of now, I updated to Jenkins version : 2.190.2.
I use jenkins 2.164.2,and i can invoke jenkinsServer#getJobXml to get jobXml,but when i invoke jenkinsServer#createJob("my-test-job", jobXml, true) occurs the following error message:
org.apache.http.client.HttpResponseException: Internal Server Error at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11) at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:359)
as anyone can help me?