I'm trying to trigger Jenkins build from C#, using this link from here. I'm trying to invoke a paramaterised Jenkins Job from .NET.
In the Jenkins job, under the "Build Triggers' section, the "Trigger builds remotely (e.g., from scripts)" is checked and an authentication token is provided like 
In my C# code, I'm asked to provide a username and an API token to invoke the Jenkins build. (_username, _apiToken from below)
private static HttpWebRequest CreateHttpRequest(string URLName)
{
//Creating HTTP web request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(URLName);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
byte[] credentialBuffer =
new UTF8Encoding().GetBytes(
_username + ":" +
_apiToken);
httpWebRequest.Headers["Authorization"] =
"Basic " + Convert.ToBase64String(credentialBuffer);
httpWebRequest.PreAuthenticate = true;
return httpWebRequest;
}
This API token, I get it from the Jenkins server by navigating to [Jenkins server URL]/me/configure and I provide this in my code.
Now, when I try to invoke this Jenkins job which has this remote build trigger enabled, I get Forbidden/Not found error via code. When I try to manually navigate that URl with the API token, it says invalid token. Could this be because of the 'Authentication Token' that I mentioned earlier?
I'm asking this, because if I uncheck that option and invoke the Jenkins Job from code, there is no issue at all.
The API Token located within jenkins/me/configure is what you use to authenticate yourself when making REST calls against the Jenkins API, as stated in the tooltip:
This API token can be used for authenticating yourself in the REST API call.
The Authenticiation Token is a way to provide a shorthand URL so that you can easily trigger builds, as stated in the tooltip:
Enable this option if you would like to trigger new builds by accessing a special predefined URL (convenient for scripts).
If you're calling Jenkins via REST there there's no need for you to use the Authenticiation Token, however you will require your API token to authenticate yourself as the one making the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With