python2中的TFS Rest Api授权问题

2024-06-28 10:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图访问Python2中的TFSrestapi,但是得到了401 Authorization Error。我可以使用相同的凭据从web-browser访问API url。同样的凭证也适用于.Net代码。尝试按this solution中的指导使用urllib2库。有什么建议在Python2中访问TFS api?在

tfs.py公司

import requests
from requests.auth import HTTPDigestAuth

username = '<UserName>'
password = '<Password>'
tfsApi = 'https://{myserver}/tfs/collectionName/_apis/projects?api-version=2.0'

tfsResponse = requests.get(tfsApi, auth=(username, password))
if(tfsResponse.ok):
    tfsResponse = tfsResponse.json()
    print(tfsResponse)
else:
    tfsResponse.raise_for_status()

错误:

^{pr2}$

.Net工作代码:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", AppConfig.TFS_API_USER, AppConfig.TFS_API_PASS))));

Tags: 代码importauthapinetusernamepasswordrequests
2条回答

TFS使用NTLM authentication protocol,因此我必须使用requests库用httpntlm身份验证更新代码。在

工作代码:

import requests
from requests_ntlm import HttpNtlmAuth

username = '<DOMAIN>\\<UserName>'
password = '<Password>'
tfsApi = 'https://{myserver}/tfs/collectionName/_apis/projects?api-version=2.0'

tfsResponse = requests.get(tfsApi,auth=HttpNtlmAuth(username,password))
if(tfsResponse.ok):
    tfsResponse = tfsResponse.json()
    print(tfsResponse)
else:
    tfsResponse.raise_for_status()
  1. 似乎您希望使用restapi get a list of team projects。API应该如下所示:

-

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
  1. 请确保已为TFS启用基本身份验证:

    • 请检查您的IIS以查看基本身份验证服务角色是否 已安装。在
    • 转到IIS管理器,选择Team Foundation Server身份验证 并禁用除基本身份验证之外的所有功能。然后做 对于TeamFoundationServer下的tfs节点也是如此。在
    • 重新启动IIS。在

enter image description here

enter image description here

相关问题 更多 >