2016-08-22 41 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