Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Octokit.net进行身份验证,并使用LibGit2Sharp推送到GitHub存储库_Git_Github_Github Api_Libgit2sharp_Octokit.net - Fatal编程技术网

使用Octokit.net进行身份验证,并使用LibGit2Sharp推送到GitHub存储库

使用Octokit.net进行身份验证,并使用LibGit2Sharp推送到GitHub存储库,git,github,github-api,libgit2sharp,octokit.net,Git,Github,Github Api,Libgit2sharp,Octokit.net,我使用LibGit2Sharp库克隆/推送经过身份验证的用户的GitHub存储库,但LibGit2Sharp不接受访问令牌来授权用户 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials() { Username = "username", Password = "password" }; 所以我有一个来自Octokit.NE

我使用LibGit2Sharp库克隆/推送经过身份验证的用户的GitHub存储库,但LibGit2Sharp不接受访问令牌来授权用户

 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
        {
            Username = "username",
            Password = "password"
        };
所以我有一个来自Octokit.NET的访问令牌,我想用这个令牌通过LibGit2Sharp推送到授权用户存储库

有什么办法可以做到这一点吗?如果我知道的话,Octokit.NET此时无法执行推送请求

更新

正如Carlos Martín Nieto和Ivan Zuzak所建议的,我修改了代码:

 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
    {
        Username = "the-access-token",
        Password = string.Empty
    };
在我使用推送逻辑之后:

 var pushOptions = new PushOptions() { Credentials = credentials};

 Remote remote = repo.Network.Remotes["origin"];

 LibGit2Sharp.Signature author = new LibGit2Sharp.Signature("name", "email", DateTime.Now);

 repo.Network.Push(remote,"HEAD",@"refs/heads/master",pushOptions, author, null);
这个逻辑在硬编码的用户名和密码上运行良好,但现在我在最后一行遇到了一个异常,它表示“LibGit2Sharp.LibGit2SharpException:Request失败,状态代码为403”

我在实现中遗漏了一些东西,或者我在Push()方法中犯了错误


多谢各位

libgit2sharp与git一样,支持在GitHub中使用身份验证令牌。您将令牌作为用户名传入,并将密码留空


git不支持仅使用HTTP Basic Auth的身份验证令牌,因此GitHub通过接受令牌作为用户名来实现这一点。只需像在命令行上使用它一样使用它。

尝试提供您的令牌作为用户名和空密码:我更改了凭据,但我有一个403错误:(您是否确保允许推送令牌?它是否与命令行git一起工作?您是对的,我必须使用“repo”范围以推送用户存储库。谢谢!:)