0

I am trying to create a task with the Jenkins API. The user has permissions but there is no way. The error that throwing is 403.

public void CreateJobWihtXmlTemplateAndRazorRemplaceWhitAutoritazion()
{
    var configData = File.ReadAllText(configCobol);
    byte[] credentialBuffer = new UTF8Encoding().GetBytes("user" + ":" +"pass");
    var webClient = new WebClient();
    webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(credentialBuffer));
    webClient.UseDefaultCredentials = true;
    webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;

    var jenkinsJobData = FactoryDataProvider.CreateJenkinsJobData();
    var result = Razor.Parse(configData, jenkinsJobData);

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(result);
    const string FORMAT = "http://{0}:{1}/createItem?name={2}";
    var path =
    string.Format(
    FORMAT,
    "someserver.example.com",
    "8080",
    jenkinsJobData.ProjectName);

    webClient.Headers["content-type"] = " application/xml";
    string response = webClient.UploadString(
    path,
    "POST",
    result
    );
    Assert.AreEqual(HttpStatusCode.OK, HttpStatusCode.OK);
}  
R15
  • 13,982
  • 14
  • 97
  • 173
Gaston Paolo
  • 138
  • 1
  • 9
  • `The user has permissions` The user obviously does not have the correct permissions, which is why you're getting a 403. – Dan Wilson Aug 02 '18 at 16:04
  • The user is an administrator, or is permission given to the api in another way? – Gaston Paolo Aug 02 '18 at 17:38
  • The second component of your authorization header appears to be a password. Have you created an API token for the account? The last time I wrote an API client for Jenkins I used API tokens. – Dan Wilson Aug 02 '18 at 17:40
  • try with the token but give 401. I'm using LDAP users – Gaston Paolo Aug 02 '18 at 18:12
  • Fixed this is the problem. https://stackoverflow.com/questions/44711696/spinnaker-403-no-valid-crumb-was-included-in-the-request – Gaston Paolo Aug 02 '18 at 18:43

1 Answers1

0

This is the FIX

Uncheck "Prevent Cross Site Request Forgery exploits"

Gaston Paolo
  • 138
  • 1
  • 9