到 GIT 的 C# 连接失败
C# connection to GIT fails
我有一个生成文件的应用程序需要存储在 ADO 的 GIT 存储库中。我正在尝试连接到存储库,但它无法给我一个 GIT 客户端。这是我的初始测试控制台应用程序(基于 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();
}
}
}
}
}
所以我尝试了各种 URI,所有这些都在 Edge 中的某个地方,但是 none 给了我一个 gitClient。这是输出:
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.
那么我应该使用什么作为 URI?还是有其他问题,可能与我获得的凭据有关?
您提到的页面明确指出,当代码中未提供凭据时,应显示一个询问凭据的交互式对话框。但是,如果您的控制台应用程序使用 .NET Standard,则不会显示该对话框。在这种情况下,您需要在身份验证代码中指定个人访问令牌。这些令牌在 Azure 网页上生成:用户设置 -> 个人访问令牌。
下面的代码片段工作正常。请确保您的令牌具有读写代码权限,否则您将无法将文件推送到存储库。
// 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}")
}
}
我有一个生成文件的应用程序需要存储在 ADO 的 GIT 存储库中。我正在尝试连接到存储库,但它无法给我一个 GIT 客户端。这是我的初始测试控制台应用程序(基于 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();
}
}
}
}
}
所以我尝试了各种 URI,所有这些都在 Edge 中的某个地方,但是 none 给了我一个 gitClient。这是输出:
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.
那么我应该使用什么作为 URI?还是有其他问题,可能与我获得的凭据有关?
您提到的页面明确指出,当代码中未提供凭据时,应显示一个询问凭据的交互式对话框。但是,如果您的控制台应用程序使用 .NET Standard,则不会显示该对话框。在这种情况下,您需要在身份验证代码中指定个人访问令牌。这些令牌在 Azure 网页上生成:用户设置 -> 个人访问令牌。
下面的代码片段工作正常。请确保您的令牌具有读写代码权限,否则您将无法将文件推送到存储库。
// 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}")
}
}