2016-08-22 44 views
2

我想使用一个控制台工具,从微软VSTS SDK的REST API得到一个TFS 2015年3更新服务器上所有现有的构建定义:如何使用VSTS仪表板小部件中的REST API从TFS获取构建定义?

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:资源不可用于匿名访问。客户认证需要。

我在做什么错?

当我送的铬合金开发者控制台的GET请求,我得到正确的响应:=/

$获得(“HTTP:// *****:8080/TFS/TestReporting /DashboardWidgets/_apis/build/definitions?api-version=2.2").success(function(res){console.log(res)})

+2

你指定在扩展清单范围:https://www.visualstudio.com/en-us/docs/integrate/extensions/develop/add-dashboard-widget#step- 2存取VSTS资源? –

+0

好的,那很尴尬。谢谢,埃迪,我真的忘了声明vso.build范围。你提出了我的希望!虽然不幸的是增加了范围并不能解决问题。我重新安装了扩展,但问题依然存在。 – simarust

+2

发现我的错误。根本不允许请求**所有**构建定义。调用 buildClient.getDefinitions(“projectName”) 完美地工作。然而,非常感谢您的帮助,特别是埃迪提醒我设置范围...;) – simarust

回答

0

这可能是您需要启用备用凭据。请参阅此链接:https://binary-stuff.com/post/how-to-enable-alternate-credentials-in-visual-studio-online-vso

而且也是这个链接可以用于以正确的方式设置的认证非常有帮助:https://www.visualstudio.com/en-us/docs/integrate/get-started/auth/overview

+0

感谢您的帮助,但我试图让扩展工作在自己的TFS服务器上,而不是VS在线。在网上VS也出现问题,所以我认为这是我自己的扩展,导致问题。我的个人访问令牌设置为“所有范围”。 = / – simarust

1

根据错误信息,您可能需要验证到TFS REST API。

VSTS和TFS有不同的身份验证方法,都可以通过PowerShell来实现。

为了在脚本TFS进行认证,就可以和密码(掩蔽作为秘密变量)通过PowerShell中传递一个用户名PSCredential的对象,并使用-Credential开关调用时REST方法。作为下方的例子:

$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