【问题标题】:Azure DevOps API for create new projects and pipeline用于创建新项目和管道的 Azure DevOps API
【发布时间】:2021-05-19 03:44:13
【问题描述】:

在我的组织中有很多研究人员,每个人都有很少的项目。对于每个项目,我必须在 Azure DevOps 和管道中创建一个存储库。我正在使用 .NET Core 3.1,但我可以更新到 .NET5

除了名称之外,每个项目的管道都是相同的。我要创建的是一个简单的内部网站,每个研究人员都可以在其中添加其项目编号,并通过 API 自动调用 Azure DevOps,以创建新的存储库和新的管道。

我尝试从 GitHub 上的 Microsoft repository 运行项目,但示例不起作用。我想要实现的是一段非常简单的代码,它可以创建一个带有关联管道的新存储库。

我在同一个Microsoft repository 中看到了分支OAuthWebSampleAspNetCore.csproj。根据Microsoft documentation,有一个OAuthController 似乎是正确的,但它不起作用。以下代码创建对 Azure DevOps 的请求。

private String BuildAuthorizationUrl(String state)
{
    UriBuilder uriBuilder = new UriBuilder(this.Settings.AuthorizationUrl);
    var queryParams = HttpUtility.ParseQueryString(uriBuilder.Query ?? String.Empty);

    queryParams["client_id"] = this.Settings.ClientApp.Id.ToString();
    queryParams["response_type"] = "Assertion";
    queryParams["state"] = state;
    queryParams["scope"] = this.Settings.ClientApp.Scope;
    queryParams["redirect_uri"] = this.Settings.ClientApp.CallbackUrl;

    uriBuilder.Query = queryParams.ToString();

    return uriBuilder.ToString();
}

这是应用程序调用的 URL

https://app.vssps.visualstudio.com:443/oauth2/authorize?client_id=myClientId&response_type=Assertion&state=6ca228c6-f73f-48be-9a0a-38c8f2483837&scope=myListOfScopes&redirect_uri=https%3a%2f%2flocalhost%3a43742%2foauth%2fcallback

这是 Azure DevOps 的结果。

下一部分是回调。基于Microsoft documentation,我必须将令牌URL https://app.vssps.visualstudio.com/oauth2/token 称为POST。以下代码是我如何执行请求的。

public async Task<ActionResult> Callback(String code, Guid state)
{
    TokenViewModel tokenViewModel = new TokenViewModel() { OAuthSettings = this.Settings };

    string error;
    if (ValidateCallbackValues(code, state.ToString(), out error))
    {
        // Exchange the auth code for an access token and refresh token
        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, this.Settings.TokenUrl);
        requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        Dictionary<String, String> form = new Dictionary<String, String>()
                {
                    { "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
                    { "client_assertion", GetClientAppSecret() },
                    { "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
                    { "assertion", code },
                    { "redirect_uri", this.Settings.ClientApp.CallbackUrl }
                };
        requestMessage.Content = new FormUrlEncodedContent(form);

        HttpResponseMessage responseMessage = await s_httpClient.SendAsync(requestMessage);

        if (responseMessage.IsSuccessStatusCode)
        {
            String body = await responseMessage.Content.ReadAsStringAsync();

            Token token = s_authorizationRequests[state];
            JsonConvert.PopulateObject(body, token);

            tokenViewModel.Token = token;
        }
        else
        {
            error = responseMessage.ReasonPhrase;
        }
    }
    else
    {
        tokenViewModel.Error = error;
    }

    return View("TokenView", tokenViewModel);
}

尽管我认为我有一个有效的令牌,并且IsSuccessStatusCode 始终是false,因为BadRequest

我可以使用任何更新的示例吗?有人遇到过同样的问题吗?

【问题讨论】:

    标签: c# asp.net-core .net-core azure-devops asp.net-core-3.1


    【解决方案1】:

    https://app.vssps.visualstudio.com:443/oauth2/authorize?client_id=myClientId&response_type=Assertion&state=6ca228c6-f73f-48be-9a0a-38c8f2483837&scope=myListOfScopes&redirect_uri=https%3a%2f%2flocalhost%3a43742%2fauth %2f回调

    我发现您使用了您的 localhost 作为您的回调 URL。

    您不能使用您的本地主机作为回调 url,因为您的本地主机不可用于 azure devops 服务。

    您必须使用可以从公共网络访问的回调 url。否则你会遇到400-bad request以上的错误。

    【讨论】:

    • 谢谢,但是如何测试我的代码?如何在我的机器上开发解决方案?
    • @Enrico 你可以考虑使用ngrok.com。或者创建一个空的 azure web 应用作为回调 url 来获取代码。
    • @Enrico 嗨,这个案子怎么样?如果您认为它有资格作为答案,您能否接受上述答案
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2023-03-26
    • 2020-11-24
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多