'C# connection to GIT fails

I have an application that generates file that needs to be stored in a GIT repository in ADO. I'm trying to connect to a repository but it fails to give me a GIT client. Here's my initial test console app (based on https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/dotnet-client-libraries?view=azure-devops)

using System;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;

namespace AdoInteractionSandbox
{
    class Program
    {
        const string projectName = "MyGreatProject";
        const string repoName = "MyRepo";

        static void Main(string[] args)
        {
            SimpleConnectionTest();

            Console.WriteLine("Finished. Hit return to continue.");
            Console.Read();
        }

        private static void SimpleConnectionTest()
        {
            string[] connectionURIs = new string[] {
                "https://dev.azure.com/myCompany",
                "https://dev.azure.com/myCompany/REPOS",
                "https://dev.azure.com/myCompany/REPOS/_git",
                "https://dev.azure.com/myCompany/REPOS/_git/MyFiles"
            };

            var creds = new VssBasicCredential(); // using default credentials for the user

            foreach (string uri in connectionURIs)
            {
                try
                {
                    Console.WriteLine($"Connecting to Azure DevOps Services at {uri}");
                    var connection = new VssConnection(new Uri(uri), creds);

                    Console.WriteLine($"Getting a GitHttpClient to talk to the Git endpoints");
                    var gitClient = connection.GetClient<GitHttpClient>();

                    Console.WriteLine($"Getting data about a specific repository");
                    var repo = gitClient.GetRepositoryAsync(projectName, repoName).Result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{uri} : {ex.Message}");
                    Console.WriteLine();
                }
            }

        }
    }
}

So I have tried it with various URI's all of which go somewhere in Edge but none of them give me a gitClient. Here's the output:

Connecting to Azure DevOps Services at https://dev.azure.com/myCompany
Getting a GitHttpClient to talk to the Git endpoints
https://dev.azure.com/myCompany : VS30063: You are not authorized to access https://dev.azure.com.

Connecting to Azure DevOps Services at https://dev.azure.com/myCompany/REPOS
Getting a GitHttpClient to talk to the Git endpoints
https://dev.azure.com/myCompany/REPOS : Page not found.

Connecting to Azure DevOps Services at https://dev.azure.com/myCompany/REPOS/_git
Getting a GitHttpClient to talk to the Git endpoints
https://dev.azure.com/myCompany/REPOS/_git : Page not found.

Connecting to Azure DevOps Services at https://dev.azure.com/myCompany/REPOS/_git/MyFiles
Getting a GitHttpClient to talk to the Git endpoints
https://dev.azure.com/myCompany/REPOS/_git/MyFiles : Page not found.

Finished. Hit return to continue.

So what should I be using as the URI? Or is there something else wrong, maybe with the credentials I am getting?



Solution 1:[1]

The page you referred to explicitly states that an interactive dialog asking for credentials should be displayed when no credentials are provided in the code. However, if your console application uses .NET Standard the dialog isn't displayed. In this case you need to specify a Personal Access Token in your authentication code. These tokens are generated on Azure web page: User Settings -> Personal Access Tokens.

Code snippet below works fine. Just make sure your token has Read & Write Code permission, otherwise you won't be able to push file into a repository.

// collectionUri, projectName, repoName and token are specified outside this block
var connection = new VssConnection(new Uri(collectionUri), new VssBasicCredential("", token));
using (var ghc = connection.GetClient<GitHttpClient>())
{
    var items = ghc.GetItemsAsync(projectName, repoName, scopePath: "/Path/To/Items").Result;
    foreach (var item in items)
    {
        Console.WriteLine($"{item.ObjectId}: {item.Path}")
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Kovalyov