【问题标题】:Azure DevOps REST APIAzure DevOps REST API
【发布时间】:2020-06-12 14:21:44
【问题描述】:

我已阅读 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 的一些示例,我将不胜感激。提前致谢!

【问题讨论】:

  • 您上面的代码正在创建一个 HTTP GET 请求,(... await client.GetAsync( ...) 而听起来您需要的是一个 HTTP POST 请求 await client.PostAsync(: blog.jayway.com/2012/03/13/…跨度>
  • 该代码是来自 Azure DevOps REST API 参考文档的 HTTP GET 请求示例。它应该返回指定组织中可用的所有存储库。然而,我的任务是创建一个 POST 请求以在 Azure DevOps 上创建一个新的存储库。我不明白如何使用 REST API,而且我似乎无法在网上找到可以帮助我解决问题的信息。
  • 我刚发现你发布了一个博客链接(起初我不太清楚,因为我是在手机上回复的。稍后我有空的时候会看看它!同时,我也希望有人有使用 Azure DevOps REST API 的经验,或者有任何可以指导我正确方向的知识。

标签: c# visual-studio .net-core web-applications azure-devops-rest-api


【解决方案1】:

您应该使用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();
                    }
                }

【讨论】:

  • requestMessage StringContent中的“xxxxxxx”应该写什么?另外,我如何在 API 控制器或 React 中使用这个 POST 方法?以及如何为新存储库的名称创建请求正文?
  • 这很棒!有什么办法可以将 StringContent 中的字符串作为 json 文件包含在其中?
  • 它就像一个魅力,谢谢!我为与 reactjs 相关的新问题创建了一个新线程,如果您对此有所了解,如果您也能帮助我,我将不胜感激。 stackoverflow.com/questions/60618173/…
猜你喜欢
  • 2021-04-19
  • 2020-04-04
  • 2022-07-17
  • 1970-01-01
  • 2020-11-26
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多