How to connect TFS Online using PAT or OAUT?
Asked Answered
S

2

6

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.com\xxxx.

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://learn.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.

Sullins answered 17/4, 2018 at 0:23 Comment(5)
How do you generate the OAuth token? Does the token has the required scope?Confiding
I used this example: learn.microsoft.com/en-us/vsts/integrate/get-started/… (at the begining, it has a link to download a c# example that works very well) I also am able to get my projects from the tfs api, scopes = ALL.Sullins
When you register the OAuth client app in VSTS and update the scope in web.config, how do you config it? Check all the scopes listed and enter all the scopes one by one in web.config file?Confiding
You might be using an app that uses config to store the scopes, that has nothing to do with Oauth, besides and I repeat; token is fine, I can execute API without problems, the only problem is while executing TF.exe and using the /loginType:OAuthSullins
@Yogurtu, were you able to ever solve this? I have some issue trying to connect to DevOps Services from TF.exe command line tool.Horotelic
C
2

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:

enter image description here


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:

enter image description here

The screenshot with /loginType and /login parameters:

enter image description here

Conchiferous answered 17/4, 2018 at 4:43 Comment(9)
Please read where I said "I might add that I already have a Pat token and an OAuth token"... your answer is about something that I already did, and explained, and It fails. Thanks.Sullins
How did you generate the OAuth token?Conchiferous
by using provided example here: learn.microsoft.com/en-us/vsts/integrate/get-started/…Sullins
Are you able to re-generate an OAuth token? As Access tokens expire relatively quickly.Conchiferous
I am able to execute stuff in TFS API, retrieve projects and stuff... so token is valid.Sullins
I use VS 2017 Enterprise, and I add a screenshot here for your reference.Conchiferous
Do me a favor, remove the /login and the /LoginType from your execution, and let me know if it keeps working, if it keeps working it means is not using your oauth token, is using the user you logged in on your visual studio, I read that this tf.exe also can read credentials from windows credentials store, If I run this on my configured machine, works fine, if I run it on a machine where I just have visual studio installed, without login, I have this problem. ThanksSullins
Are you able to get the command working by following my screenshots?Conchiferous
I'm running into the same problem, more than 2 years later. My PAT is much shorter than in the screen shot above, but it works fine via the REST API, is not expired, is granted all scopes. I'm using the dev.azure.com URL, but the old *.visualstudio.com URL doesn't work for me either. And the REST API can't handle large changesets. So I'm stuck. No way to check in a large changeset using a PAT...Episode
E
0

As per 2023, the following snippet may help:

MY_PAT=yourPAT # replace "yourPAT" with "PatStringFromWebUI"

B64_PAT=$(printf ":%s" "$MY_PAT" | base64)

git -c http.extraHeader="Authorization: Basic ${B64_PAT}" clone https://dev.azure.com/yourOrgName/yourProjectName/_git/yourRepoName

as described in this link,

And you can get the "yourPAT" from this link

Exam answered 30/7, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.