我试图在TFS 2中访问Python REST,但获取401 Authorization Error。我可以使用相同的凭据从web-browser访问API。同样的凭据也适用于.Net代码。使用urllib2库在这个解决方案中进行了尝试。是否建议在Python2中访问TFS?
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()错误:
Traceback (most recent call last):
File "D:\Scripts\tfs.py", line 13, in <module>
tfsResponse.raise_for_status()
File "C:\Python27\lib\site-packages\requests\models.py", line 862, in raise_fo
r_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://{myserver}/tfs/collectionName/_apis/projects?api-version=2.0.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))));发布于 2016-10-20 09:09:38
TFS使用NTLM认证协议,因此我必须使用请求库使用HTTP身份验证更新代码。
工作代码:
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()发布于 2016-10-20 08:19:17
-
http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0- check your IIS to see whether the Basic authentication service role is installed.
- go to IIS Manager, select Team Foundation Server -- Authentication and disable everything other than Basic Authentication. Then do the same for the tfs node under Team Foundation Server.
- restart your IIS.


https://stackoverflow.com/questions/40146361
复制相似问题