如何在VSTS仪表板小部件中使用RESTAPI从TFS获取构建定义?
我正在尝试使用Microsofts VSTS SDK的REST API在仪表板小部件中获取TFS 2015 Update 3服务器上的所有现有构建定义:如何在VSTS仪表板小部件中使用RESTAPI从TFS获取构建定义?,rest,tfs,authorization,azure-devops,azure-devops-rest-api,Rest,Tfs,Authorization,Azure Devops,Azure Devops Rest Api,我正在尝试使用Microsofts VSTS SDK的REST API在仪表板小部件中获取TFS 2015 Update 3服务器上的所有现有构建定义: VSS.init({ explicitNotifyLoaded: true, usePlatformStyles: true }); VSS.require("TFS/Dashboards/WidgetHelpers", "TFS/Build/RestClient", "VSS/
VSS.init({
explicitNotifyLoaded: true,
usePlatformStyles: true
});
VSS.require("TFS/Dashboards/WidgetHelpers", "TFS/Build/RestClient", "VSS/Authentication/Services"],
function (WidgetHelpers, TFS_Build_Api) {
VSS.register("BuildStatusMonitor.Configuration", function () {
return {
load: function (widgetSettings, widgetConfigurationContext) {
var buildClient = TFS_Build_Api.getClient();
buildClient.getDefinitions().then(function(definition) {
//
}, function(reason) {
// 401
});
},
}
});
VSS.notifyLoadSucceeded();
});
不幸的是,我总是得到一份工作
TFS.WebApi.Exception:TF400813:资源不可用于匿名访问。需要客户端身份验证
我做错了什么
当我在chromes developer控制台上发送get请求时,我得到了正确的响应:=/
$.get(“http://*****:8080/tfs/TestReporting/DashboardWidgets/_-api/build/definitions?api version=2.2”).success(函数(res){console.log(res)})
您可能需要启用备用凭据。请参阅此链接:
此外,此链接对于以正确的方式设置身份验证非常有用:根据错误信息,您可能需要对TFS REST API进行身份验证 VST和TFS有不同的身份验证方法,都可以通过PowerShell实现 为了在脚本中使用TFS进行身份验证,您可以通过PowerShell将用户名和密码(伪装为秘密变量)传递到PSCredential对象中,并在调用REST方法时使用-Credential开关。举例如下:
$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 $Uri
当用户没有访问构建rest客户端的权限时,另一种选择是使用PAT身份验证。在使用VSS-SDK和Typescript/Javascript时,可以使用以下技术设置PAT身份验证。按照这些步骤生成的PAT直接粘贴到“{PAT}”中,没有“:”
import service = require("VSS/Service");
import buildRestClient = require("TFS/Build/RestClient");
import { BuildHttpClient } from "TFS/Build/RestClient";
import { VssHttpClient } from "VSS/WebApi/RestClient";
import { BasicAuthTokenManager } from "VSS/Authentication/Services";
...
const buildClient: BuildHttpClient =
service.getClient(buildRestClient.BuildHttpClient);
(buildClient as VssHttpClient).authTokenManager =
new BasicAuthTokenManager("", "{PAT}");
// Subsequent calls will now use PAT authentication
const builds: Build[] = await buildClient.getBuilds(projectId);
您是否在扩展清单中指定了范围:?好的,这很尴尬。谢谢,Eddie,我真的忘记声明vso.build范围了。你激发了我的希望!不幸的是,添加范围并不能解决问题。我重新安装了分机,但问题仍然存在。我发现了我的错误。根本不允许请求所有构建定义。调用buildClient.getDefinitions(“projectName”)非常有效。但非常感谢您的帮助,尤其是埃迪提醒我设置范围……;)感谢您的帮助,但我正在尝试让扩展在自己的TFS服务器上工作,而不是在VS online上。在VS online上也会出现问题,所以我认为是我的扩展本身导致了问题。我的个人访问令牌设置为“所有作用域”=/