简体   繁体   English

检索 Azure DevOps REST API 5.1 的访问令牌

[英]Retrieve Accesstoken for Azure DevOps REST API 5.1

In our app we want to retrieve an accesstoken.在我们的应用程序中,我们想要检索一个访问令牌。 With this token we want to perform certain actions with Azure DevOps rest API 5.1.使用此令牌,我们希望使用 Azure DevOps rest API 5.1 执行某些操作。

I've tried the suggested documententation from Microsoft for OAuth2 ( https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops ) and PAT's.我已经尝试了 Microsoft 为 OAuth2 ( https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops ) 和 PAT 提供的建议文档。 This always gives back a 203 response as an result.结果总是返回 203 响应。 Does somebody know a workaround for this issue?有人知道这个问题的解决方法吗?

According to my test, we can call Azure DevOps rest API with Azure Ad access token.根据我的测试,我们可以使用 Azure Ad 访问令牌调用 Azure DevOps rest API。 For more details, please refer to the following steps 1. Register an Azure AD application详情请参考以下步骤 1. 注册 Azure AD 应用程序在此处输入图片说明

  1. Configure applications配置应用程序

    a.一种。 Create client secret创建客户端密钥

    b.Configure permissions配置权限

    在此处输入图片说明

  2. Get token获取令牌

 # 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. Call Rest API调用 Rest API 在此处输入图片说明

For how to get authenticated for calling API, you may refer to: Authenticate调用API如何通过认证,可以参考: Authenticate

Here is the sample for using OAuth2 with ADAL: https://github.com/microsoft/azure-devops-auth-samples/blob/master/ManagedClientConsoleAppSample/Program.cs以下是将 OAuth2 与 ADAL 结合使用的示例: https : //github.com/microsoft/azure-devops-auth-samples/blob/master/ManagedClientConsoleAppSample/Program.cs

And here is a quick sample by using PAT:这是使用 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());
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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