Retrieve Accesstoken for Azure DevOps REST API 5.1

烈酒焚心 提交于 2021-02-18 19:00:08

问题


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.

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. This always gives back a 203 response as an result. Does somebody know a workaround for this issue?


回答1:


According to my test, we can call Azure DevOps rest API with Azure Ad access token. For more details, please refer to the following steps 1. Register an Azure AD application

  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



回答2:


For how to get authenticated for calling API, you may refer to: Authenticate

Here is the sample for using OAuth2 with ADAL: https://github.com/microsoft/azure-devops-auth-samples/blob/master/ManagedClientConsoleAppSample/Program.cs

And here is a quick sample by using 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());
            }
        }
    }


来源:https://stackoverflow.com/questions/59609591/retrieve-accesstoken-for-azure-devops-rest-api-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!