I know little to none about Rest API calls so bear with me. I am trying to queue a TFS 2017 build using Rest API through Powershell. I tried using the TFS API but found out that will not be able to work for me. This is what I have:
$Uri = "http://MyTFS:8080/tfs/DefaultCollection/Project/_apis/build/builds?api-version=3.0"
$TFSAPIKeyForAutomatedBuild = SecretKey
$body = @"
{
"definition":
{
"ID": "BuildID"
},
"RequestedFor":
{
"Id":"MyID"
}
}
"@
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $TFSAPIKeyForAutomatedBuild")
$buildresponse = Invoke-RestMethod -Method Post -header $headers -ContentType application/json -Uri $Uri -Body (ConvertTo-Json $Body)
However, when I run this, I get the error:
TF400813: Resource not available for anonymous access. Client authentication required
As an aside, I have already been able to queue a build using Postman so it should work one way or another.
Any suggestions would be appreciated.
This is what fixed my issue. You have to encode the Token used to connect to TFS like so:
$encodedPAT = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$TFSAPIKeyForAutomatedBuild"))
$Uri = "http://MyTfs:8080/tfs/DefaultCollection/Projects/_apis/build/builds?api-version=3.0"
$body = @{ definition = @{id = BuildID} }
$bodyJson = ConvertTo-Json $body
write-host $bodyJson
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $encodedPAT")
$buildresponse = Invoke-RestMethod -Method Post -header $headers -ContentType application/json -Uri $Uri -Body (ConvertTo-Json $body)
I also changed the format of my json because it was throwing an error regarding deserialization.