toon vanhoutte

During Azure DevOps build and release pipelines, you might have the need to consult the Azure DevOps REST API.  This blog explains how you can easily perform the authentication that is required to call that REST API.

Enable OAuth Authentication

First of all, you need to check the option Allow scripts to the OAuth token.  This enables scripts and other processes launched by tasks to access the OAuth token through the System.AccessToken variable.  This setting is somewhere hidden in the Additional options of the Agent Job:

Access1

Use the OAuth token inside the script

Within a PowerShell script you can now retrieve the System.AccessToken variable and use it to authenticate against the Azure DevOps REST API.  A simplified example:

#Set authorization headers Write-Host Set authorization headers $headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" } #Invoke REST API Write-Host Invoke REST API Invoke-RestMethod $url -Method $method -Body $body -Headers $headers -ContentType 'application/json'…

View original post 90 more words

Posted by Callon Campbell [MVP]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s