[英]TFS Rest Api authorization issue in Python 2
I am trying to access TFS
REST API in Python
2 but getting 401 Authorization Error
. 我正在尝试在
Python
2中访问TFS
REST API,但收到401 Authorization Error
。 I am able to access API url from web-browser
using same credentials. 我可以使用相同的凭据从
web-browser
访问API网址。 Also same credentials is working with .Net
code. 同样的凭据也可以与
.Net
代码一起使用。 Tried with urllib2 library as guided in this solution . 按照此解决方案中的说明尝试使用urllib2库。 Any suggestion to access TFS api in Python2?
有任何建议在Python2中访问TFS api吗?
tfs.py 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()
Error: 错误:
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 Working code: .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 uses NTLM authentication protocol , Hence I have to update my code with HTTP NTLM authentication using the requests library. TFS使用NTLM身份验证协议 ,因此,我必须使用请求库通过HTTP NTLM身份验证来更新代码。
Working code: 工作代码:
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()
- -
http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
Make sure you have enabled Basic Auth for your TFS: 确保为TFS启用了基本身份验证:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.