jenkinsci / java-client-api

A Jenkins API client for Java
MIT License
902 stars 469 forks source link

Forbiddeden exception while calling createJob() #428

Open kumarshorav11 opened 5 years ago

kumarshorav11 commented 5 years ago

Hey , I am getting this issue using 0.3.8 version of this maven dependency. I am able to get the all job from jenkins server and also job details. However, when I am trying to create a new job by using

config.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <actions />
    <description>test job from java api</description>
    <keepDependencies>false</keepDependencies>
    <properties />
    <scm class="hudson.scm.NullSCM" />
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false
    </blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers />
    <concurrentBuild>false</concurrentBuild>
    <builders />
    <publishers />
    <buildWrappers />
    **<crumb>XXX</crumb>
    <crumbRequestField>Jenkins-Crumb</crumbRequestField>**
</project>

and then

try {
            jServer.createJob("test-job", "src/main/resources/config.xml",true);
        }catch(Exception e) {
            e.printStackTrace();
        }

I am getting below error

Error :

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:359)
    at com.xxx.App.main(App.java:34)
kumarshorav11 commented 5 years ago

With the same user name and password, I am able to get all available jobs in jenkins.

JenkinsServer jServer = new JenkinsServer(new URI(JENKINS_URL), JENKINS_USER_NAME,JENKINS_PASSWORD);
        Map<String, Job> jobs = jServer.getJobs();
        /*
         * jobs.forEach((k, v) -> { System.out.println("Key : " + k.toString() +
         * "-----------Value ->" + v.getFullName()); });
         */

        JobWithDetails job = jobs.get(JENKINS_JOB_NAME).details();
        job.getBuilds().forEach(b->System.out.println("Build Details : "+b.getNumber()));