【发布时间】:2017-08-07 23:29:02
【问题描述】:
我正在尝试从 C# 触发 Jenkins 构建,使用来自 here. 的此链接我正在尝试从 .NET 调用参数化的 Jenkins 作业。
在 Jenkins 作业中,在“构建触发器”部分下,选中“远程构建触发器(例如,从脚本)”并提供身份验证令牌,如
在我的 C# 代码中,我被要求提供用户名和 API 令牌来调用 Jenkins 构建。 (_username, _apiToken 来自下面)
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;
}
这个 API 令牌,我通过导航到 [Jenkins 服务器 URL]/me/configure 从 Jenkins 服务器获取它,并在我的代码中提供它。
现在,当我尝试调用启用了此远程构建触发器的此 Jenkins 作业时,我通过代码收到 Forbidden/Not found 错误。当我尝试使用 API 令牌手动导航该 URl 时,它显示无效令牌。这可能是因为我之前提到的“身份验证令牌”吗?
我问这个,因为如果我取消选中该选项并从代码中调用 Jenkins 作业,则根本没有问题。
【问题讨论】: