如何以编程方式登录VSTS

问题描述 投票:3回答:1

我有一个自动分支实用程序,可以执行各种操作。我们已升级到VSTS,我在使用新服务器的Tfs调用时遇到问题:

//login

NetworkCredential netCred = new NetworkCredential("emailAddress", password");

BasicAuthCredential basicCred = new BasicAuthCredential(netCred);

TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);

tfsCred.AllowInteractive = false;

TfsTeamProjectCollection tfsServer = new TfsTeamProjectCollection(new Uri("https://myco.visualstudio.com/mycoll"), tfsCred);

// get a local workspace

VersionControlServer sc = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

... other code

然后热潮! “您无权访问https://myco.visualstudio.com/mycoll

某处有什么设置吗?

我应该尝试使用REST API吗?

我打电话给我不应该做的事吗?

我已经为URI尝试了各种格式,使用:8080,路径中的/ tfs无效!

tfs azure-devops tfs-sdk
1个回答
1
投票

您可以使用PAT(个人访问令牌)访问VSTS。在VSTS中单击您的配置文件,然后转到安全菜单项。您可以在那里定义PAT。保持PAT保存,因为它只会显示一次。

enter image description here

根据您的需要定义范围enter image description here

您可以使用PAT访问TFS REST API,如以下PowerShell示例所示。使用https://yourAccount.visualstudio.comhttps://yourAccount.visualstudio.com/DefaultCollection作为ColletionUri参数是可以的。

param(
    [Parameter(Mandatory=$true)]
    [string] $token,
    [Parameter(Mandatory=$true)]
    [string] $collectionUri
)



$User=""

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$token)));
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};

$Uri = $collectionUri + '/_apis/projects?api-version=1.0'

$projects = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Uri -Headers $header

你可以在这里找到C#代码示例https://www.domstamand.com/accessing-tfs-2017-programmatically/

© www.soinside.com 2019 - 2024. All rights reserved.