git - 在 libgit2sharp 中使用 TFS PAT 授权

标签 git tfs libgit2 libgit2sharp

我想使用 LibGit2Sharp 访问内部部署的 TFS 服务器 GIT 存储库。到目前为止,我只是在玩这个库,我无法让它像我期望的那样工作。

我的第一个问题是使用个人访问 token 进行授权。我可以使用提供我公司电子邮件和密码的 UsernamePasswordCredentials 克隆存储库,但是,我找不到使用 PAT 执行此操作的方法。我找不到关于它的任何可靠信息,所以我尝试以不同的组合使用它,比如用它替换我的密码/用户名,但我无法让它工作 - 我总是得到 LibGit2Sharp.LibGit2SharpException: 'too many redirects或身份验证重播”。

这应该意味着凭据无效,但我确定我的 token 有效并且我授权了所有范围,因此它应该能够使用我的凭据做我能做的一切。有没有我可以遵循的示例来启动和运行它?这是我当前使用用户名和密码的解决方案:

        var co = new CloneOptions();
        co.CredentialsProvider = (_url, _user, _cred) =>
            new UsernamePasswordCredentials
            {
                Username = "username@companyname.com",
                Password = "password"
            };

        Repository.Clone(@"remoteUrl-received-from-api", "destination-folder", co);

我的另一个问题是,从这个 API 端点接收到哪个 URL:TFS API传递给 LibGit2Sharp 的是正确的吗?我正在使用“remoteUrl”(这是 TFS 网络应用程序 URL)进行克隆,并且在提供用户名和密码时它可以工作,但是,Repository.IsValid 返回 false。我对 TFS 和 GIT 不是很熟悉,所以请原谅我在这方面完全缺乏知识。

如果我明天无法正常工作,我将不得不使用 cmd.exe 进程和控制台命令,我真的很想避免这种情况 - 所以我们将不胜感激任何帮助。

最佳答案

在 libgit2sharp 中,似乎传递了 token ,因为带有空白密码的用户名特定于 GitHub。这可能不适用于 TFS/VSTS 托管的 git 存储库。

查看 GITHUB 中的相关问题:Does Git-Clone support Personal access tokens ?

对于 TFS/VSTS,如果您不想使用 UsernamePasswordCredentials 提供您的公司电子邮件和密码,您可以使用备用身份验证凭据

enter image description here

对于 url,您应该使用 TFS git remoterepo url。更多详细示例请引用此链接:How do you authenticate to VSTS using LibGit2Sharp?

您可以使用备用凭据对 VSTS 进行身份验证。以下代码显示了如何向 VSTS 进行身份验证并执行克隆作业。

using LibGit2Sharp;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string surl = "https://youraccount.visualstudio.com/DefaultCollection/_git/Remoterepo";
            string lpath = "C:\\LocalPath";
            CloneOptions co = new CloneOptions();
            string un = "alternativeusername";
            string pwd = "alternativepassword";
            Credentials ca = new UsernamePasswordCredentials() {Username = un, Password = pwd };
            co.CredentialsProvider = (_url, _user, _cred) => ca ;
            Repository.Clone(surl,lpath,co);
        }
    }
}

关于git - 在 libgit2sharp 中使用 TFS PAT 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671730/

相关文章:

git - 在 git clone 之后运行 bash 脚本

java - 如何从源代码编译 Android 运行时 (ART)

sharepoint - TFS 基于需求的套件在 MTM 中显示为测试套件或 Web 访问测试套件——它们是可配置的 SharePoint 列表吗?

tfs - 什么是 VSTS 链接设置功能?

ruby - `git diff --name-only master`怎么用三防?

c - libgit2 git_checkout_head 和 GIT_CHECKOUT_SAFE 对工作目录没有任何作用

git - 克隆 git 或 hg 存储库时如何节省磁盘使用量?

git - 我可以让 "git status"显示未跟踪文件的文件大小吗?

database - 从 TFS 迁移到 VSTS - 数据库警告 : Ignore or Abort

macos - 使用brew构建libgit2静态库