我試圖訪問TFS
Python
2中的REST API,但獲取401 Authorization Error
。我可以使用相同的憑據從web-browser
訪問API網址。代碼也與.Net
相同。在this solution的指導下嘗試使用urllib2庫。任何建議在Python2中訪問TFS api?Python 2中的TFS Rest 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 – Roopendra