My cloud service is trying to create a new team in VSTS using TFS APIs. Here is the code snippet -
var serverUrl = TfsTeamProjectCollection.GetFullyQualifiedUriForName("https://vsts_instace_name.visualstudio.com/DefaultCollection");
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(serverUrl,
new AadCredential(username, password));
teamProjectCollection.EnsureAuthenticated();
var vssCredentials = teamProjectCollection.ClientCredentials.ConvertToVssCredentials(teamProjectCollection.Uri);
ProjectHttpClient projectHttpClient = new ProjectHttpClient(teamProjectCollection.Uri,
vssCredentials);
var projectInfo = projectHttpClient.GetProject("TestProject");
var teamService = teamProjectCollection.GetService<TfsTeamService>();
var team = teamService.CreateTeam(projectInfo.Id.ToString(), "NewTeam", string.Empty, null);
The code works correctly in the local environment but fails in cloud. An exception is thrown at the line where it tries to retrieve the projectInfo
. Exception is - TF400813: Resource not available for anonymous access. Client authentication required.
Do I need to make any change in the authentication code?
Create a Personal Access Token or enable Alternative authentication credentials and then trying with following code:
string tfsurl = "https://xxxxxx.visualstudio.com/";
string projname = "project";
NetworkCredential nc = new NetworkCredential("alternativeusername", "alternativepasswordorpersionalaccesstoken");
BasicAuthCredential bac = new BasicAuthCredential(nc);
TfsClientCredentials tfcc = new TfsClientCredentials(bac);
tfcc.AllowInteractive = false;
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfsurl), tfcc);
ttpc.Authenticate();
ProjectHttpClient phc = ttpc.GetClient<ProjectHttpClient>();
TeamProject pi = phc.GetProject(projname).Result;