I want to connect to a onebranch/git repo and read a file, possibly make edit and checkin. My question is how can we do that? How will user authentication work in this case?
Asked
Active
Viewed 814 times
-2
-
Welcoome to StackOverflow! When asking questions, you should post what code you have already written, so it is easier to fix your problem. It doesn't have to be the full code, only the part you think is relevant to your problem. This way, answers can also adapt better to your existing code. – Green Apr 28 '20 at 07:28
1 Answers
0
You should first connect to Azure DevOps Server or Azure DevOps Service.
Personal Access Token authentication for REST services
public static void PersonalAccessTokenRestSample()
{
// Create instance of VssConnection using Personal Access Token
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssBasicCredential(string.Empty, pat));
}
OAuth Authentication for REST services
public static void OAuthSample()
{
// Create instance of VssConnection using OAuth Access token
VssConnection connection = new VssConnection(new Uri(collectionUri), new VssOAuthAccessTokenCredential(accessToken));
}
More details for Auth please refer Auth samples for Azure DevOps Services.
Then you could connect Git Repo and select a Branch to list files or a file. A code snippet from similar question: How to list all the files that are in TFS GIT repo using REST API
List<GitRepository> repositories = await client.GetRepositoriesAsync(true); // or use GetRepositoryAsync()
var repo = repositories.FirstOrDefault(r => r.Name == "Some.Repo.Name");
GitVersionDescriptor descriptor = new GitVersionDescriptor()
{
VersionType = GitVersionType.Branch,
Version = "develop",
VersionOptions = GitVersionOptions.None
};
List<GitItem> items = await client.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.Full, versionDescriptor: descriptor);
More detail samples showing how to extend and integrate with Azure DevOps Server and Azure DevOps Services using the .NET client libraries.

PatrickLu-MSFT
- 49,478
- 5
- 35
- 62