【问题标题】:Authentication Error Connecting to TFS through Powershell通过 Powershell 连接到 TFS 的身份验证错误
【发布时间】: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


【解决方案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 格式,因为它在反序列化方面引发了错误。

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 1970-01-01
    • 2018-11-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2012-12-01
    相关资源
    最近更新 更多