javagitauthorizationjgit

JGit authorization problems [Caused by: org.eclipse.jgit.api.errors.TransportException: https://github.com/ ... .git: not authorized]


My very-very simple test code always fails. I cannot get access to my own repository (PRIVATE) (it is completely new and empty). The username and password are perfectly working I tested signing in and out several times. I went through several StackOverflow posts, but still no clue what can cause the problem. I have tired two different ways, first clone the repo (give the credentials at that point) and then push. My second try was to create a repo commit something than push (give the credentials at this point). None of these worked.

JGit error message:

Exception in thread "main" org.eclipse.jgit.api.errors.TransportException: https://github.com/USERNAME/testing.git: not authorized
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:249)
    at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:325)
    at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:191) 

Java code (First way):

    public static void main(String[] args) throws GitAPIException, IOException {

        CredentialsProvider credentialsProvider
                = new UsernamePasswordCredentialsProvider( "XXXXXXXXXX", "XXXXXXXXXX" );
        Git git = Git.cloneRepository()
                .setURI( "XXXXXXXXXX" )
                .setDirectory(new File(System.getProperty("user.home") + File.separator + "git_test"))
                .setCredentialsProvider( credentialsProvider )
                .call();

        File file = new File( git.getRepository().getWorkTree(), "file" + new Object().hashCode() );
        file.createNewFile();
        git.add().addFilepattern( file.getName() ).call();
        git.commit().setMessage( "Add file " + file.getName() ).call();

        git.push()
                .setCredentialsProvider( credentialsProvider )
                .call();
    }
}

Java code (Second way):

this.git = Git.init().setDirectory(new File(folderPath)).call();

this.git.add().addFilepattern("temp.txt").call();
        this.git.commit().setMessage("Modified temp.txt").call();

this.git.push().setRemote(textfield.getText())
               .setCredentialsProvider(new UsernamePasswordCredentialsProvider("XXXXXXX", "XXXXXX"))
               .call();

Do you have any clue what is going on?


Solution

  • According to Github: "For developers, if you are using a password to authenticate Git operations with GitHub.com today, you must begin using a personal access token over HTTPS (recommended) or SSH key by August 13, 2021, to avoid disruption. If you receive a warning that you are using an outdated third-party integration, you should update your client to the latest version." Described here

    Thus, I had to generate a token, what I did the following way: A personal access token (classic) generation...

    After, in the setCredentialsProvider() method I gave the following:

    .setCredentialsProvider( new UsernamePasswordCredentialsProvider( "<token>", "" ) );
    

    Password must be left empty, just give the token and that's it.