1
votes

I have developed a TFS extension for TFS 2017 on premises. I need to get a list of the service endpoint within a project

I am using the following code inside a TFS extension (code-hub)

private callTfsApi() {
    const vsoContext = VSS.getWebContext();
    let requestUrl = vsoContext.host.uri
        + vsoContext.project.id
        + "/_apis/distributedtask/serviceendpoints?api-version=3.0-preview.1";

    return VSS.getAccessToken().then(function (token) {
        // Format the auth header
        const authHeader = VSS_Auth_Service.authTokenManager.getAuthorizationHeader(token);

        // Add authHeader as an Authorization header to your request
        return $.ajax({
            url: requestUrl,
            type: "GET",
            dataType: "json",
            headers: {
                "Authorization": authHeader
            }
        }).then((response: Array<any>) => {

            console.log(response);
        });
    });
}

On every request the server responds with a status of 401 (Unauthorized).

If I use postman and basic authentication the call to the service endpoints APIs works.

Also, using the same code but a different API call (projects) works.

let requestUrl = vsoContext.host.uri + "_apis/projects?api-version=1.0"; 

Is there some sort of known bug related to the service endpoints APIs or maybe the extension must specify a scope? (not sure which one to include though)

2
What's the detail of your error message info? Did you get a Invalid Resource error? - PatrickLu-MSFT
Print out the token and use it in Postman to get the service endpoint, does it work? - Eddie Chen - MSFT
That is exactly what I did and is not working with service endpoints. The same bearer token works with projects. - Juan M. Elosegui
Have you tried the scope I mentioned? - Eddie Chen - MSFT
I will give it a try after March 5. Thanks for the suggestion!! - Juan M. Elosegui

2 Answers

0
votes

Service endpoints are created at project scope. If you could query project info, you should also be able to query this.

You could try to add related scope vso.project in https://docs.microsoft.com/en-us/vsts/extend/develop/manifest#scopes page see if this do the trick.

Another way to narrow down this issue is directly using Rest API to call from code (not inside a TFS extension ) to see if the issue is related to extension side.

0
votes

Add scope: vso.serviceendpoint_query