如何使用 c# API 授权 Azure Devops?
How to Authorize Azure Devops using c# API?
我已经在 VS 中安装了“Microsoft.TeamFoundationServer.Client”包并创建了 DevOps 工作项表单。我想使用以下代码将自定义规则添加到工作项表单。当 运行 下面的代码时,我得到 'result' 值作为 html content
输出:“Azure DevOps 服务 | 登录”
Code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://dev.azure.com/orgname/_apis/work/processes/processid/workItemTypes/Microsoft.VSTS.WorkItemTypes.Epic/rules?api-version=6.0-preview.2");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = @"{
'name': 'customRule',
'conditions': [
{
'conditionType': '$when',
'field': 'Custom.parentdrpdwn',
'value': 'value1'
}
],
'actions': [
{
'actionType': '$setDefaultValue',
'targetField': 'Custom.childrpdwn',
'value': 'value2'
}
],
'isDisabled': false
}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
由于此身份验证问题,我无法 post https 请求,我们如何以相同的方法授权 Azure DevOps 并在我的组织中创建自定义业务规则?
Azure DevOps API 使用基本身份验证。你只需要添加一个 header:
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("username:password");
string patEncoded = Convert.ToBase64String(bytes);
httpWebRequest.Headers.Add("Authorization", "Basic " + patEncoded);
您可以使用您的用户名 + 密码或生成个人访问令牌 (PAT)。对于 PAT,将用户名留空。
我们需要在网页中create PAT token,然后在代码中使用。这是一个示例。
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
我们可以参考文档 Get started sample and Get started with the REST APIs 了解更多详情。
我已经在 VS 中安装了“Microsoft.TeamFoundationServer.Client”包并创建了 DevOps 工作项表单。我想使用以下代码将自定义规则添加到工作项表单。当 运行 下面的代码时,我得到 'result' 值作为 html content
输出:“Azure DevOps 服务 | 登录”
Code:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://dev.azure.com/orgname/_apis/work/processes/processid/workItemTypes/Microsoft.VSTS.WorkItemTypes.Epic/rules?api-version=6.0-preview.2");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = @"{
'name': 'customRule',
'conditions': [
{
'conditionType': '$when',
'field': 'Custom.parentdrpdwn',
'value': 'value1'
}
],
'actions': [
{
'actionType': '$setDefaultValue',
'targetField': 'Custom.childrpdwn',
'value': 'value2'
}
],
'isDisabled': false
}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
由于此身份验证问题,我无法 post https 请求,我们如何以相同的方法授权 Azure DevOps 并在我的组织中创建自定义业务规则?
Azure DevOps API 使用基本身份验证。你只需要添加一个 header:
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("username:password");
string patEncoded = Convert.ToBase64String(bytes);
httpWebRequest.Headers.Add("Authorization", "Basic " + patEncoded);
您可以使用您的用户名 + 密码或生成个人访问令牌 (PAT)。对于 PAT,将用户名留空。
我们需要在网页中create PAT token,然后在代码中使用。这是一个示例。
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
我们可以参考文档 Get started sample and Get started with the REST APIs 了解更多详情。