【发布时间】:2017-10-13 07:16:54
【问题描述】:
我想访问托管在 VSTS 中的存储库以及相应存储库中的每个分支。下面是我的代码
public class VSTSController : ApiController
{
const String c_collectionUri = "https://****.visualstudio.com/DefaultCollection";
const String c_projectName = "****";
const String c_repoName = "****";
[HttpGet]
[Route("api/VSTS/Init")]
public string Init()
{
// Interactively ask the user for credentials, caching them so the user isn't constantly prompted
VssCredentials creds = new VssClientCredentials();
creds.Storage = new VssClientCredentialStorage();
// Connect to VSTS
VssConnection connection = new VssConnection(new Uri(c_collectionUri), creds);
// Get a GitHttpClient to talk to the Git endpoints
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
// Get data about a specific repository
var repo = gitClient.GetRepositoryAsync(c_projectName, c_repoName).Result;
return repo.RemoteUrl;
}
}
当我运行这段代码时,我得到一个异常Microsoft.VisualStudio.Services.Common.VssUnauthorizedException' occurred in mscorlib.dll but was not handled in user code。
如何对 VSTS REST API 进行身份验证?我无法理解关注这个documentation。
【问题讨论】:
-
对于网页应用,您需要使用 OAuth 或直接指定凭证(令牌或替代帐户)。 (正如丹尼尔所说)
标签: azure-devops azure-devops-rest-api