【问题标题】:Retrieve Accesstoken for Azure DevOps REST API 5.1检索 Azure DevOps REST API 5.1 的访问令牌
【发布时间】:2020-04-23 20:22:02
【问题描述】:

在我们的应用程序中,我们想要检索一个访问令牌。使用此令牌,我们希望使用 Azure DevOps rest API 5.1 执行某些操作。

我已经尝试过 Microsoft 为 OAuth2 (https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops) 和 PAT 提供的建议文档。结果总是返回 203 响应。有人知道这个问题的解决方法吗?

【问题讨论】:

  • 你有这方面的代码吗? 203 也是“203 非权威信息”,所以有点成功 - 也许?
  • 嗨,欢迎来到 SstackOverflow!您能否更新有关您的问题的更多信息?对于 203 响应代码,您正在尝试哪一步并显示?更好地分享更多步骤细节::-)
  • 您能否有机会尝试我的解决方案,看看是否有帮助?

标签: c# azure oauth-2.0 azure-devops


【解决方案1】:

根据我的测试,我们可以使用 Azure Ad 访问令牌调用 Azure DevOps rest API。更多详情,请参考以下步骤 1.注册一个Azure AD应用

  1. 配置应用程序

    一个。创建客户端密码

    b.配置权限

  2. 获取令牌

 # get code
GET  https://login.microsoftonline.com/{tenant}/oauth2/authorize?
client_id=<your app client id>
&response_type=code
&redirect_uri=<>
&response_mode=query
&resource=499b84ac-1321-427f-aa17-267ca6975798
&state=12345

#Get token
Post https://login.microsoftonline.com/{tenant}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=<>
&code=<>
&redirect_uri=https%3A%2F%2Flocalhost%3A12345
&resource=499b84ac-1321-427f-aa17-267ca6975798
&client_secret=<>
  1. 调用休息 API

【讨论】:

    【解决方案2】:

    调用API的认证方法可以参考:Authenticate

    这里是使用 OAuth2 和 ADAL 的示例:https://github.com/microsoft/azure-devops-auth-samples/blob/master/ManagedClientConsoleAppSample/Program.cs

    这是一个使用 PAT 的快速示例:

        class Program
        {
            public static string personalaccesstoken = "erxvtg*****6aljqa";
            public static string organization = "jack0503";
            public static string project = "keyvault";
    
            static void Main(string[] args)
            {
                Console.ReadLine();
                GetProjects();
                Console.ReadLine();
            }
    
    
            public static async void GetProjects()
            {
                try
                {
                    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/projects"))
                        {
                            response.EnsureSuccessStatusCode();
                            string responseBody = await response.Content.ReadAsStringAsync();
                            Console.WriteLine(responseBody);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 1970-01-01
      • 2021-11-15
      • 2023-03-15
      • 1970-01-01
      • 2020-06-16
      • 2019-10-24
      • 2020-01-08
      • 2015-04-06
      相关资源
      最近更新 更多