我对Rest API调用知之甚少,所以请耐心等待。我试图通过Powershell使用Rest API排队TFS 2017版本。我尝试使用TFS API,但发现无法为我工作。这就是我所拥有的:
$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)
然而,当我运行它时,我收到错误:
TF400813:资源不可用于匿名访问。客户 需要身份验证
顺便说一下,我已经能够使用Postman对构建进行排队,因此它应该以某种方式工作。
任何建议都将不胜感激。
答案 0 :(得分:1)
这就解决了我的问题。您必须对用于连接到TFS的令牌进行编码,如下所示:
$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)
我还改变了json的格式,因为它引发了关于反序列化的错误。