Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

post - How to Authorize Azure Devops using c# API?

I have installed "Microsoft.TeamFoundationServer.Client" package in VS and creating DevOps Work item forms. I want to add custom Rules to the work item form using below code. When running below code I am getting the 'result' value as html content

output: "Azure DevOps Services | Sign In"

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();
                } 
 

because of this authentication issue I am not able to post the https request ,how can we authorize the Azure DevOps in the same method and create custom business rule in my Org ?

question from:https://stackoverflow.com/questions/65913497/how-to-authorize-azure-devops-using-c-sharp-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We need to create PAT token in the web page and then use it in the code. Here is an sample.

string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

We could refer to the doc Get started sample and Get started with the REST APIs for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...