Partager via


Using PAT token in ReleaseManagement REST API's

Generating PAT Token:-

Step1: Generate PAT token by visiting your profile and selecting the right ReleaseManagement scope as per API need ( see 'Available scopes' section https://www.visualstudio.com/en-us/integrate/extensions/develop/manifest )

Step2: Copy the token generated after clicking 'Create Token' as shown in image above

 

Code which uses the generated PAT token above :-

 public static async Task<string> GetReleaseDefinitionsUsingPATToken()
        {
            var username = "nobody";
            var token = "<Give your PAT Token here>";

            var url = "https://{accountname}.visualstudio.com/{projectname}/_apis/release/definitions?api-version=3.0-preview.1";

            using (HttpClient client = new HttpClient())
            {
                var mediaTypeHeader = new MediaTypeWithQualityHeaderValue("application/json");
                client.DefaultRequestHeaders.Accept.Add(mediaTypeHeader);

                var credentialBytes = Encoding.ASCII.GetBytes($"{username}:{token}");
                var encodedCredentialBytes = Convert.ToBase64String(credentialBytes);
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encodedCredentialBytes);

                var response = await client.GetAsync(url);
                var body = await response.Content.ReadAsStringAsync();
                return body;
            }
        }