如何使用 PAT 或 OAUT 连接 TFS Online? [英] How to connect TFS Online using PAT or OAUT?

查看:14
本文介绍了如何使用 PAT 或 OAUT 连接 TFS Online?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不敢相信我被登录卡住了:(讨厌这种情况发生.

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

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

TF.exe 工作区/collection:xxxx.visualstudio.com/xxxx/loginType:OAuth/login:.,MyPatTokenOrMyOauthToken/noprompt

我收到以下回复:

<块引用>

TF30063:您无权访问 xxxx.visualstudio.comxxxx.

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

最后,我可能会更改所有内容以更改 TFS api 的 tf.exe,但我无法在 api 中找到相同的方法(请参阅参考:

<小时>

更新:

我用新账号测试过,结果如下.如果我删除 /loginType/login 参数,会弹出一个窗口要求我登录.

没有/loginType/login参数的截图:

带有/loginType/login参数的截图:

Can't believe I'm stuck with a LOGIN :( hate when this happens.

Can somebody enlight me how to connect TF.EXE by using PAT password or in the best case an OAuth token?

I might add that I already have a Pat token and an OAuth token, not a problem while trying to get those, but every time I try this example:

TF.exe workspaces /collection:xxxx.visualstudio.com/xxxx /loginType:OAuth /login:.,MyPatTokenOrMyOauthToken /noprompt

I get the following response:

TF30063: You are not authorized to access xxxx.visualstudio.comxxxx.

So, I Know command it's ok, because if I don't specify a login, a modal window prompts for credentials, and I tested already with that approach and works fine.

For the end, I might change everything to change tf.exe for the TFS api, but I'm unable to find same methods in the api (see reference: https://docs.microsoft.com/es-es/rest/api/vsts/?view=vsts )

If API has same methods than TF.exe, that will be useful, but so far I don't see same methods in the API.

Hope somebody has the solution for my problem.

Thanks in advance.

解决方案

From my test, PAT token doesn't work in the following command, you have to get a OAuth token:

tf workspaces /collection:https://xxxx.visualstudio.com /loginType:OAuth /login:.,[OAuth token]

For the api that authenticate with Visual Studio Team Services (VSTS), you could refer to the examples in this link:

Here is an example getting a list of projects for your account:

REST 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;
    }   
}

.Net Client Libraries

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;                    
}


Add a screenshot:


Update:

I've tested with a new account, and the result is as below. If I remove /loginType and /login parameters, a window will pop up to ask me logon.

The screenshot without /loginType and /login parameters:

The screenshot with /loginType and /login parameters:

这篇关于如何使用 PAT 或 OAUT 连接 TFS Online?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆