3

I am trying to create a job on Jenkins using java client (https://github.com/jenkinsci/java-client-api) by calling .createJob(String jobName, String configXml) . However, Jenkins server throws 403 forbidden error.

Sample Code :

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);

Now, error I am getting :

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.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:359)
    at com.hcl.OffByTwoJenkins.main(OffByTwoJenkins.java:31)

Jenkins Server security : When I select "Any user can do any thing", job creation is successful. However, when I select "Logged In user can do any thing", I am getting the above error. Moreover, even though I am sending correct user and password, with all permission to create job ( able to create a job using Jenkins web UI). What permission or setting change is required to achieve this.

Thanks

3 Answers 3

0

To resolve this issue try by unchecking "Prevent Cross Site Request Forgery exploits" in jenkins.com/configureSecurity section and it started working.

1
  • Sorry, this is already unchecked Sukhjinderji. I did that already but no luck.
    – Kumar
    Nov 1, 2019 at 6:54
0

Use an API token to create JenkinsHttpClient to avoid this issue.

JenkinsHttpClient jenkinsHttpClient = new JenkinsHttpClient(new URI("http://192.168.0.11:8080"),
            "admin", "your api token");

You can generate an api token from jenkins dashboard (Jenkins> Users > Select a user> settings > add new api token).

0

For me, using true for crumbFlag helped: e.g. you should call jenkins.createJob("test-nov1", sourceXML, true); instead of jenkins.createJob("test-nov1", sourceXML);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.