3

I use the LibGit2Sharp library to clone/push the authenticated user's GitHub repository, but LibGit2Sharp doesn't accept an access token to authorize the user.

 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
        {
            Username = "username",
            Password = "password"
        };

So I have an access token comes from Octokit.NET and I want to use this token to push to the authorized user repository with LibGit2Sharp.

Are there any way to do this trick? If I know well, Octokit.NET couldn't perform push requests at this time.

UPDATE

As Carlos Martín Nieto and Ivan Zuzak suggest, I modified the code:

 LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
    {
        Username = "the-access-token",
        Password = string.Empty
    };

And after I use my push logic:

 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);

This logic works fine with the hard-coded username and password, but now I have got an exception at the last line, says " LibGit2Sharp.LibGit2SharpException: Request failed with status code: 403"

I missed something from the implementation or I made a mistake at Push() method?

Thank you very much!

Gábor Domonkos
  • 1,081
  • 1
  • 17
  • 33
  • 2
    Try providing your token as the username, and an empty password: https://help.github.com/articles/git-automation-with-oauth-tokens#step-2-clone-a-repository – Ivan Zuzak Jul 03 '14 at 15:58

2 Answers2

3

libgit2sharp supports using authentication tokens with GitHub the same way git does. You pass in the token as the username and leave the password blank.

git does not have any support for authentication tokens over simply using HTTP Basic Auth, so GitHub have implemented this by accepting the token as the username. Simply use it the same way you'd use it on the command line.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16
0

Please refer the following tutorial to generate access token code.

https://catalyst.zoho.com/help/tutorials/githubbot/generate-access-token.html

While connecting to github, please use username and the access token code. It works 100%.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – alberand Nov 27 '21 at 20:31
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30448598) – Gino Mempin Nov 28 '21 at 02:54