5
votes

I'm trying to get all existing build definitions on a TFS 2015 Update 3 server using the REST API from Microsofts VSTS SDK in a dashboard widget:

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();
});

Unfortunately I always get a

TFS.WebApi.Exception: TF400813: Resource not available for anonymous access. Client authentication required.

What am I doing wrong?

When I am sending a get request on chromes developer console, I get the correct response: =/

$.get("http://*****:8080/tfs/TestReporting/DashboardWidgets/_apis/build/definitions?api-version=2.2").success(function(res) { console.log(res) })

3
Did you specify the scope in extension manifest: visualstudio.com/en-us/docs/integrate/extensions/develop/…?Eddie Chen - MSFT
OK, that's embarrassing. Thanks, Eddie, I really forgot to declare the vso.build scope. You raised my hopes! Though unfortunately adding the scope does not solve the problem. I reinstalled the extension but the problem remains.simarust
Found my mistake. It's simply not allowed to request all build definitions. Calling buildClient.getDefinitions("projectName") works perfectly. Yet many thanks to your help especially to Eddie for reminding me of setting the scope... ;)simarust

3 Answers

0
votes

It might be that you need to enable alternate credentials. See this link: https://binary-stuff.com/post/how-to-enable-alternate-credentials-in-visual-studio-online-vso

And also this link can be quite helpfull for setting the authentication in a correct manner: https://www.visualstudio.com/en-us/docs/integrate/get-started/auth/overview

0
votes

According to the error info, you may need to authenticate to the TFS REST API.

VSTS and TFS have different authentication methods, both can be achieved through PowerShell.

In order to authenticate with TFS in a script, you can pass a username and password (masked as a secret variable) through PowerShell into a PSCredential object and use the -Credential switch when invoking the REST method. A example as below :

$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
0
votes

When the user does not have the privileges to access the build rest-client, then the alternative is to use PAT authentication. You can use the following technique, to set PAT authentication, when using the VSS-SDK and Typescript/Javascript. The PAT generated following the steps here was pasted directly into "{PAT}" without the ':'.

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);