繁体   English   中英

Azure DevOps REST API

[英]Azure DevOps REST API

我已阅读 Azure DevOPS REST API 文档并尝试将其多次实施到我的 Web 应用程序中,但无济于事。 我没有使用 REST API 的经验,如果有人能指导我走向正确的方向,我将不胜感激。

我正在尝试为 Azure DevOps 存储库创建一个 POST 请求,并希望通过 API 方法创建一个新的存储库。 我已经阅读了关于此的文档,但我不知道如何在我自己的项目中实现这一点。 我了解我需要如何创建到 API 的连接,但不知道我如何以及在何处为此方法编写请求正文。 我想知道如何指定新存储库的名称。 我实际上非常无能,也不知道一般如何使用 REST API。

我正在将 Visual Studio 与 .NET Core 3.0 一起使用,并计划将它与 React.js 一起使用

这是到目前为止我正在使用的代码,我不知道从哪里开始:

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

我很感激对此问题的任何澄清,以及有关如何使用 REST API 的一些示例。 提前致谢!

您应该使用POST方法来创建存储库。 在此处检查 API:

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

代码应如下所示:

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

更新:

       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