简体   繁体   English

Python 2中的TFS Rest Api授权问题

[英]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()
  1. It seems you want to get a list of team projects with REST API. 看来您想使用REST API 获得团队项目的列表 The API should look like: 该API应该如下所示:

- -

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0
  1. Make sure you have enabled Basic Auth for your TFS: 确保为TFS启用了基本身份验证:

    • check your IIS to see whether the Basic authentication service role is installed. 检查您的IIS,以查看是否安装了基本身份验证服务角色。
    • go to IIS Manager, select Team Foundation Server -- Authentication and disable everything other than Basic Authentication. 转到IIS管理器,选择“ Team Foundation Server-身份验证”,然后禁用“基本身份验证”以外的所有功能。 Then do the same for the tfs node under Team Foundation Server. 然后对Team Foundation Server下的tfs节点执行相同的操作。
    • restart your IIS. 重新启动您的IIS。

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM