简体   繁体   中英

Creating a project in Azure Devops with rest api

I would like to create an Azure DevOps project via the REST API ( see documentation ) but I can't get it up and running.

I tried sending the request in PostMan but I don't know how to authenticate via OAuth2 (documentation).

Here is what I tried so far:


Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace WorkItemTest
{
    class AzureAdmin
    {
        private readonly Uri uri;
        private readonly string personalAccessToken;

        public AzureAdmin(string orgName, string personalAccessToken)
        {
            this.uri = new Uri("https://dev.azure.com/" + orgName);
            this.personalAccessToken = personalAccessToken;
        }

        public async Task<bool> createProject()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            Encoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", personalAccessToken))));
                    string contentString = "{\"name\":\"sup\",\"description\":\"\",\"visibility\":0,\"capabilities\":{\"versioncontrol\":{\"sourceControlType\":\"Git\"},\"processTemplate\":{\"templateTypeId\":\"b8a3a935-7e91-48b8-a94c-606d37c3e9f2\"}}}";
                    HttpContent content = new StringContent(contentString);
                    var result = await client.PostAsync($"{uri}/_apis/projects?api-version=6.0", content);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return true;
        }
    }
}


Response

400: Bad request

Daniel Mann is right. The issue is with json. If you create an object and post it directly, all is fine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace WorkItemTest
{
    class AzureAdmin
    {
        private readonly Uri uri;
        private readonly string personalAccessToken;

        public AzureAdmin(string orgName, string personalAccessToken)
        {
            this.uri = new Uri("https://dev.azure.com/" + orgName);
            this.personalAccessToken = personalAccessToken;
        }

        public async Task<bool> createProject()
        {

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            Encoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", "", personalAccessToken))));

                    var req = new Root
                    {
                        name = "sup3",
                        description = "",
                        visibility = 0,
                        capabilities = new Capabilities
                        {
                            versioncontrol = new Versioncontrol {sourceControlType = "Git"},
                            processTemplate = new ProcessTemplate
                            {
                                templateTypeId = "b8a3a935-7e91-48b8-a94c-606d37c3e9f2"
                            }
                        }
                    };

                    var result = await client.PostAsJsonAsync($"{uri}/_apis/projects?api-version=6.0", req); //
                    Console.WriteLine(result.StatusCode);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return true;
        }

        public class Versioncontrol
        {
            public string sourceControlType { get; set; }
        }

        public class ProcessTemplate
        {
            public string templateTypeId { get; set; }
        }

        public class Capabilities
        {
            public Versioncontrol versioncontrol { get; set; }
            public ProcessTemplate processTemplate { get; set; }
        }

        public class Root
        {
            public string name { get; set; }
            public string description { get; set; }
            public int visibility { get; set; }
            public Capabilities capabilities { get; set; }
        }

    }
}

I tried sending the request in PostMan but I don't know how to authenticate via OAuth2 (documentation).

For this , you can use PAT token to authenticate in PostMan.

在此处输入图片说明

On how to create a PAT in azure devops ,please refer to this document .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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