TFS (Team Foundation Server) API是微软提供的用于与TFS或Azure DevOps Server交互的编程接口。通过REST API可以访问团队项目中的各种资源,包括源代码、工作项、构建等。
TFS/Azure DevOps提供了REST API来访问版本控制中的文件和文件夹。
GET https://{instance}/{collection}/{project}/_apis/git/repositories/{repository}/items?scopePath={path}&recursionLevel={level}&api-version=6.0
参数说明:
instance
: TFS服务器地址collection
: 集合名称project
: 团队项目名称repository
: Git仓库名称scopePath
: 要列出的路径(如/
表示根目录)recursionLevel
: 递归级别(Full, OneLevel, OneLevelPlusNestedEmptyFolders)using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string collectionUrl = "https://dev.azure.com/{organization}";
string project = "YourProject";
string repository = "YourRepository";
string personalAccessToken = "your-pat-token";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", personalAccessToken))));
string requestUrl = $"{collectionUrl}/{project}/_apis/git/repositories/{repository}/items?scopePath=/&recursionLevel=Full&api-version=6.0";
using (HttpResponseMessage response = await client.GetAsync(requestUrl))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
}
import requests
import base64
# 配置信息
organization = "your-organization"
project = "YourProject"
repository = "YourRepository"
pat_token = "your-pat-token"
# 构建请求URL
url = f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository}/items"
params = {
"scopePath": "/",
"recursionLevel": "Full",
"api-version": "6.0"
}
# 设置认证头
credentials = f":{pat_token}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
headers = {
"Authorization": f"Basic {encoded_credentials}",
"Content-Type": "application/json"
}
# 发送请求
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
print(response.json())
else:
print(f"Error: {response.status_code} - {response.text}")
原因: 未提供有效的PAT (Personal Access Token) 或认证头设置不正确
解决方案:
原因:
解决方案:
原因: PAT没有足够的权限访问资源
解决方案:
没有搜到相关的沙龙