We are migrating from TFS 2015 to TFS 2017. We have developed an extension for TFS 2017 on premises, which calls VSTS Rest APIs to fetch workitems from the TFS Server. The Web API calls were working fine in TFS 2015, but gives 401 Unauthorized error in TFS 2017. Even, I am getting the same error while I try to use TFS_Wit_WebApi.getClient().getWorkItem() method. I am using below code with authorization token to consume the service.
VSS.getAppToken().then(function(token){
authHeader = VSS_Auth_Service.authTokenManager.getAuthorizationHeader(token);
getData(authHeader);
});
function getData(authHeader)
{
var projectName = 'My Project';
var webContext = VSS.getWebContext();
var collectionUrl = webContext.host.uri + webContext.project.name +
"_apis/wit/wiql?api-version=1.0";
var d = { "query": "Select [System.Id] from WorkItems Where [System.WorkItemType] = 'Requirement' and [System.TeamProject] = '" + projectName + "' and [State] <> 'Closed' and [State] <> 'Proposed'" };
$.ajax({
type: 'POST',
url : collectionUrl,
contentType: 'application/json',
data: JSON.stringify(d),
headers: {
"Authorization": authHeader
},
success: function (data) {
for (i = 0; i < data.workItems.length; i++) {
$.ajax({
type: 'GET',
url: data.workItems[i].url,
contentType: 'application/json',
headers: {
"Authorization": authHeader
},
success: function (data) {
item = data.id + "-" + data.fields["System.Title"];
},
error: function (msg, url, line) {
alert(msg);
}
});
}
;
}, // Success Function
error: function (msg, url, line) {
alert("Msg:"+ msg);
}
});
}