eragon2983
eragon2983

Reputation: 31

How to use Libgit2sharp authentication using Personal Access Token

For more information. I have already cloned the private repository in my local drive. More background info. This is a web application which im trying to implement in which user can download different files from my local cloned folders of the git repo. So everytime someone tries to download im trying to fetch and pull to get the latest data in my local drive.

        var path = "my local drive path";
        using (var repo = new Repository(path))
        {
            FetchOptions options = new FetchOptions();
            options.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) =>
                new UsernamePasswordCredentials()
                {
                    Username="[email protected]",
                    Password = "mypassword"
                });

            foreach (Remote remote in repo.Network.Remotes)
            {
                IEnumerable<string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
                Commands.Fetch(repo, remote.Name, refSpecs, options, logMessage);
            }
        }
        //Console.WriteLine(logMessage);

        using (var repo = new Repository(path))
        {
            // Credential information to fetch
            LibGit2Sharp.PullOptions options = new LibGit2Sharp.PullOptions();
            options.FetchOptions = new FetchOptions();
            options.FetchOptions.CredentialsProvider = new CredentialsHandler(
                (url, usernameFromUrl, types) =>
                    new UsernamePasswordCredentials()
                    {
                        Username = "[email protected]",
                        Password = "my password"
                    });

            // User information to create a merge commit
            var signature = new LibGit2Sharp.Signature(
                new Identity("xxxx", "[email protected]"), DateTimeOffset.Now);

            // Pull
            Commands.Pull(repo, signature, options);
        }

With my normal username and password it works fine. But when i use my personal access token inplace of password or username, it throws error. Can someone help me with this.

Upvotes: 3

Views: 6193

Answers (2)

Yash Sharma
Yash Sharma

Reputation: 1

CloneOptions op = new CloneOptions();
op.FetchOptions.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
{
    Username = YourUser,
    Password = YourToken
};

this works too in latest version you cant access "CredentialsProvider" , need to use "FetchOptions" .

Upvotes: 0

codeOnTheWestCoast
codeOnTheWestCoast

Reputation: 41

CloneOptions co = new CloneOptions();
string gitUser = "gitUser", gitToken = "gitToken";
co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = gitUser, Password = gitToken };

Works for me using github. Just put the git token in place of a password and still include the user name. Maybe you need to provide the correct access to your token?

Upvotes: 4

Related Questions