【问题标题】:Getting Unauthorised error while triggering TFS build from release definition从发布定义触发 TFS 构建时出现未经授权的错误
【发布时间】:2018-06-21 01:28:52
【问题描述】:

我需要在成功部署发布后触发构建。我已经尝试在发布定义的 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) 不要公开发布您的凭据。 2)通过这样做,我发现您使用了错误的分隔符。对于基本身份验证,使用冒号: 而不是反斜杠“\”分隔用户名和密码(另请参阅stackoverflow.com/a/27951845/3905079)。
  • 另外,除非 TFS 服务器为 TFS 2017 及更高版本运行 SSL,否则基本身份验证不起作用。
  • @briantist- 如果你仔细观察,我通过删除一些字符在凭据之间写了“sample”;)。只是为了展示我如何发送这样的凭据。

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


【解决方案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)"

【讨论】:

    猜你喜欢
    • 2018-11-26
    • 2016-11-27
    • 2021-06-19
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2015-12-21
    • 2017-09-26
    • 2017-04-08
    相关资源
    最近更新 更多