【问题标题】:Getting 401 error on using TFS VSTS Rest APIs from the TFS Extension从 TFS 扩展中使用 TFS VSTS Rest API 时出现 401 错误
【发布时间】:2018-11-23 02:12:47
【问题描述】:

我们正在从 TFS 2015 迁移到 TFS 2017。我们已经为本地 TFS 2017 开发了一个扩展,它调用 VSTS Rest API 从 TFS 服务器获取工作项。 Web API 调用在 TFS 2015 中运行良好,但在 TFS 2017 中出现 401 Unauthorized 错误。甚至,当我尝试使用 TFS_Wit_WebApi.getClient().getWorkItem() 方法时,我遇到了同样的错误。 我正在使用下面的代码和授权令牌来使用服务。

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

                    });
		}

【问题讨论】:

    标签: tfs azure-devops


    【解决方案1】:

    尝试改用VSS.getAccessToken() 方法。

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2021-04-02
      • 2019-01-25
      • 2017-08-20
      • 2017-08-03
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多