简体   繁体   English

Azure DevOps REST API

[英]Azure DevOps REST API

I have read the Azure DevOPS REST API documentation and tried to implement it to my Web Application multiple times but to no avail.我已阅读 Azure DevOPS REST API 文档并尝试将其多次实施到我的 Web 应用程序中,但无济于事。 I have no experience using REST API's and I would appreciate if someone could guide me into the right direction.我没有使用 REST API 的经验,如果有人能指导我走向正确的方向,我将不胜感激。

I am trying to create a POST Request for Azure DevOps Repositories and wish to create a new repository through the API method.我正在尝试为 Azure DevOps 存储库创建一个 POST 请求,并希望通过 API 方法创建一个新的存储库。 I have read the documentation on this, but I have no idea how to implement this in my own project.我已经阅读了关于此的文档,但我不知道如何在我自己的项目中实现这一点。 I understand how I need to create a connection to the API, but no idea how and where I write the Request Body for this method.我了解我需要如何创建到 API 的连接,但不知道我如何以及在何处为此方法编写请求正文。 I would like to know how I specify the name of the new repository.我想知道如何指定新存储库的名称。 I'm actually very clueless and have no idea how to use the REST API in general.我实际上非常无能,也不知道一般如何使用 REST API。

I am using Visual Studio with .NET Core 3.0 and plan to use this with React.js我正在将 Visual Studio 与 .NET Core 3.0 一起使用,并计划将它与 React.js 一起使用

Here's the code I'm working with so far, and I have no idea where to go from here:这是到目前为止我正在使用的代码,我不知道从哪里开始:

public class AzureDevOps { 
    public static async void GetRepositories()
    {
        try
        {
            var personalaccesstoken = "PAT_FROM_WEBSITE";

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", personalaccesstoken))));

                using (HttpResponseMessage response = await client.GetAsync(
                            "https://dev.azure.com/{organization}/_apis/git/repositories?api-version=5.1"))
                {
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseBody);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

I would appreciate any clarification on this matter, as well as some examples on how to use the REST API.我很感激对此问题的任何澄清,以及有关如何使用 REST API 的一些示例。 Thanks in advance!提前致谢!

You should use POST method to create a repository.您应该使用POST方法来创建存储库。 Check the API here:在此处检查 API:

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-5.1 https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-5.1

The code should look like:代码应如下所示:

                var PAT = "xxxxx";

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", PAT))));
                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1");
                    requestMessage.Content = new StringContent("{\"name\": \"RepositoryName\",\"project\": {\"id\": \"xxxxxxx\"}}", Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }

Update:更新:

       var PAT = "xxxxx";
       var body = new
            {
                name = "RepositoryName",
                project = new
                {
                    id = "xxxxxxx"
                }
            };

                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", PAT))));
                    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.1");
                    requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                    {
                        response.EnsureSuccessStatusCode();
                    }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM