如何使用 PAT 或 OAUT 连接 TFS Online?

Yog*_*rtu 5 tfs login azure-devops azure-devops-rest-api

不敢相信我被 LOGIN 困住了 :( 讨厌这种情况。

有人能告诉我如何使用 PAT 密码或在最好的情况下使用 OAuth 令牌连接 TF.EXE 吗?

我可能会补充说,我已经有一个 Pat 令牌和一个 OAuth 令牌,尝试获取它们时没有问题,但是每次我尝试这个示例时:

TF.exe workspaces /collection:xxxx.visualstudio.com/xxxx /loginType:OAuth /login:.,MyPatTokenOrMyOauthToken /noprompt
Run Code Online (Sandbox Code Playgroud)

我收到以下回复:

TF30063: 您无权访问 xxxx.visualstudio.com\xxxx。

所以,我知道命令没问题,因为如果我不指定登录名,模态窗口会提示输入凭据,我已经使用该方法进行了测试并且工作正常。

最后,我可能会更改所有内容以更改 TFS api 的 tf.exe,但我无法在 api 中找到相同的方法(请参阅参考:https : //docs.microsoft.com/es-es/rest/ api/vsts/?view=vsts )

如果 API 具有与 TF.exe 相同的方法,那将很有用,但到目前为止我没有在 API 中看到相同的方法。

希望有人能解决我的问题。

提前致谢。

Cec*_*SFT 2

根据我的测试,PAT 令牌在以下命令中不起作用,您必须获取 OAuth 令牌:

tf workspaces /collection:https://xxxx.visualstudio.com /loginType:OAuth /login:.,[OAuth token]
Run Code Online (Sandbox Code Playgroud)

对于使用 Visual Studio Team Services (VSTS) 进行身份验证的 api,您可以参考此链接中的示例:

以下是获取您帐户的项目列表的示例

休息API

using System.Net.Http;
using System.Net.Http.Headers;

...

//encode your personal access token                   
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

ListofProjectsResponse.Projects viewModel = null;

//use the httpclient
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://{accountname}.visualstudio.com");  //url of our account
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); 

    //connect to the REST endpoint            
    HttpResponseMessage response = client.GetAsync("_apis/projects?stateFilter=All&api-version=1.0").Result;

    //check to see if we have a succesfull respond
    if (response.IsSuccessStatusCode)
    {
        //set the viewmodel from the content in the response
        viewModel = response.Content.ReadAsAsync<ListofProjectsResponse.Projects>().Result;

        //var value = response.Content.ReadAsStringAsync().Result;
    }   
}
Run Code Online (Sandbox Code Playgroud)

.Net 客户端库

using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Common;

...

//create uri and VssBasicCredential variables
Uri uri = new Uri(url);
VssBasicCredential credentials = new VssBasicCredential("", personalAccessToken);

using (ProjectHttpClient projectHttpClient = new ProjectHttpClient(uri, credentials))
{
    IEnumerable<TeamProjectReference> projects = projectHttpClient.GetProjects().Result;                    
}
Run Code Online (Sandbox Code Playgroud)

添加屏幕截图:

在此输入图像描述


更新:

我用新帐户进行了测试,结果如下。如果我删除/loginType/login参数,将会弹出一个窗口要求我登录。

/loginType不带参数的截图/login

在此输入图像描述

截图/loginType/login参数:

在此输入图像描述