[英]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.我想通过 REST API( 请参阅文档)创建一个 Azure DevOps 项目,但我无法启动并运行它。
I tried sending the request in PostMan but I don't know how to authenticate via OAuth2 (documentation).我尝试在 PostMan 中发送请求,但我不知道如何通过 OAuth2(文档)进行身份验证。
Here is what I tried so far:这是我到目前为止尝试过的:
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;
}
}
}
400: Bad request 400:错误的请求
Daniel Mann is right.丹尼尔曼是对的。 The issue is with json.
问题出在 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).
我尝试在 PostMan 中发送请求,但我不知道如何通过 OAuth2(文档)进行身份验证。
For this , you can use PAT token to authenticate in PostMan.为此,您可以使用 PAT 令牌在 PostMan 中进行身份验证。
On how to create a PAT in azure devops ,please refer to this document .关于如何在 azure devops 中创建 PAT,请参阅此文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.