[英]Authentication Error Connecting to TFS through Powershell
我對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對構建進行排隊,因此它應該以一種或另一種方式工作。
任何建議,將不勝感激。
這就是解決我的問題的原因。 您必須像這樣對用於連接到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的格式,因為它引發了有關反序列化的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.