从发布定义触发TFS构建时获取未授权错误

时间:2018-01-11 16:30:06

标签: powershell tfs msbuild ms-release-management azure-devops-rest-api

我需要在成功部署版本后触发构建。我尝试在版本定义中使用Powershell中的以下代码。

执行后,我收到此错误 - 由于凭据无效而拒绝访问

$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)"`

1 个答案:

答案 0 :(得分:1)

根据我的测试,您可以使用-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)"

或者提供特定的凭据:

$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