Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Authentication Token and API Token in Jenkins remote build?

Tags:

c#

jenkins

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 this.

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.

like image 683
Ponni Avatar asked Dec 05 '25 14:12

Ponni


1 Answers

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.

like image 73
Chawin Avatar answered Dec 07 '25 04:12

Chawin