rest - 通过 Powershell 连接到 TFS 的身份验证错误
问题描述
我对 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 的格式,因为它引发了关于反序列化的错误。
推荐阅读
- tensorflow - 层 conv1 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。收到的完整形状:[None, 256, 3]
- c - 如何连接 MATLAB 和 React?
- java - 无法从 JSON 转换;意外字符('f'(代码 102)):在(字符串)“5f19a7e99933db43cb23e83d”处的根级值之间需要空格分隔
- html - 如果图像不是特定大小,如何在 CSS 中使用背景颜色?
- apache2 - 如何修复 Apache WSGI 上的 FastAPI 应用程序错误?
- python - Matplotlib 在带阴影的表面上绘制 3d 线
- android - 清除 TextView 的文本后,HorizontalScrollView 保持可滚动状态
- c - 静态链接到 C 程序的 Rust 代码是否因此获得任何有益的安全属性?
- arduino - Arduino Mega 上的这个芯片叫什么名字?我没有找到它的数据表 56 TI MSP?
- go - 创建 Couchbase 的动态 n1ql 查询,但它没有返回记录并优雅退出而没有任何错误