首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 2中的TFS Rest Api授权问题

Python 2中的TFS Rest Api授权问题
EN

Stack Overflow用户
提问于 2016-10-20 05:45:32
回答 2查看 2.6K关注 0票数 2

我试图在TFS 2中访问Python REST,但获取401 Authorization Error。我可以使用相同的凭据从web-browser访问API。同样的凭据也适用于.Net代码。使用urllib2库在这个解决方案中进行了尝试。是否建议在Python2中访问TFS?

tfs.py

代码语言:javascript
复制
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()

错误:

代码语言:javascript
复制
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工作代码:

代码语言:javascript
复制
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))));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-20 09:09:38

TFS使用NTLM认证协议,因此我必须使用请求库使用HTTP身份验证更新代码。

工作代码:

代码语言:javascript
复制
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()
票数 4
EN

Stack Overflow用户

发布于 2016-10-20 08:19:17

  1. 您似乎希望使用REST进行获取团队项目的列表。API应该如下所示:

-

代码语言:javascript
复制
http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
  1. 确保您已经为您的TFS启用了Basic:
代码语言:javascript
复制
- 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.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40146361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档