libgit2sharp icon indicating copy to clipboard operation
libgit2sharp copied to clipboard

Fetch and Pull using Personal Access token

Open vinith29 opened this issue 4 years ago • 1 comments

I'm trying to fetch and pull everytime a request come to the application. So basically im using my own username and password for the same as of now. But when i try to implement it with my personal access token, it throws error.

    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 = githubtoken //my personal token
            });

        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 = githubtoken //my personal token
                });

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

I tried replacing the token in place of the username and giving empty string in place of password. Still i'm getting the same error.

vinith29 avatar Jul 22 '20 07:07 vinith29

Check my SO answer https://stackoverflow.com/a/72602335/6158341

Kebechet avatar Jun 13 '22 11:06 Kebechet