【发布时间】:2017-08-02 17:48:56
【问题描述】:
我试图在Python 2 中访问TFS REST API,但得到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()
错误:
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))));
【问题讨论】:
-
您使用本地 TFS 还是 VSTS?您使用的是哪个版本的 TFS?
-
我正在使用
Microsoft Visual Studio Team Foundation Server,版本:14.95.25229.0