0

I need to trigger a build after successful deployment of a release. I have tried using below code in Powershell in the release definition.

After executing, I get this error - Access is denied due to invalid credentials

$url = "http://abc:8080/tfs/GlobalCollection/Project/_apis/build/builds?
 api-version=2.0"

$body = "{ 'definition' : { 'id' : 1} }"

$type = "application/json"

$headers = @{
Authorization = "Basic d3JlblxzcsampleTIzNA=="
}

Write-Host "URL: $url"

$definition = Invoke-RestMethod -Uri $url -Body $body -ContentType $type -
Method Post -Headers $headers

Write-Host "Definition = $($definition | ConvertTo-Json -Depth 1000)"`
Community
  • 1
  • 1
shenQA
  • 39
  • 1
  • 8
  • 1
    1) Don't publicly post your credentials. 2) by doing so I was able to find that you're using the wrong separator character. For basic auth, separate user name nad password with a colon `:` not a backslash "\" (see also https://stackoverflow.com/a/27951845/3905079 ). – briantist Jan 11 '18 at 16:48
  • 1
    Plus, Basic Auth doesn't work unless the TFS server is running SSL for TFS 2017 and up. – jessehouwing Jan 11 '18 at 17:02
  • 1
    @briantist- if you look closely, i wrote "sample" in between credentials by deleting some characters ;). Just to show how i'm sending credentials like this. – shenQA Jan 12 '18 at 09:59

1 Answers1

1

Based on my test, you can use -UseDefaultCredentials :

$type = "application/json"

$url = "http://abc:8080/tfs/GlobalCollection/Project/_apis/build/builds?api-version=2.0"

$body = "{ 'definition' : { 'id' : 56} }"

Write-Host "URL: $url"

$definition = Invoke-RestMethod -Uri $url -Body $body -ContentType $type -Method Post -UseDefaultCredentials

Write-Host "Definition = $($definition | ConvertTo-Json -Depth 1000)"

Alternatively provide the specific Credential:

$user = "username"
$password = "password"

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$password)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$type = "application/json"

$url = "http://abc:8080/tfs/GlobalCollection/Project/_apis/build/builds?api-version=2.0"

$body = "{ 'definition' : { 'id' : 56} }"

Write-Host "URL: $url"

$definition = Invoke-RestMethod -Uri $url -Body $body -ContentType $type -Method Post -Headers $headers

Write-Host "Definition = $($definition | ConvertTo-Json -Depth 1000)"

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55