【发布时间】:2018-11-02 21:05:53
【问题描述】:
我对 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 对构建进行排队,因此它应该以一种或另一种方式工作。
任何建议将不胜感激。
【问题讨论】:
-
您是否尝试使用“凭据”参数?
-
是的,我已经尝试了凭据参数,但我得到了同样的错误
-
您可以尝试以下方法:$User = "username";$Password = "password";$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force;$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword);$releaseresponse = Invoke-RestMethod -Method Get -Credential $credential -ContentType application/json -Uri $乌里; ?
-
是的,这就是我使用的确切代码。还是一样的错误:/
标签: rest powershell restful-authentication