user3817808
user3817808

Reputation: 187

Push to GitLab with JGit authorization error

I am able to clone from GitLab via JGit, but when I go to push the changes, I get a not authorized error message.

Three more essential details:

  1. I own the repository so it's not an issue with read-only access.

  2. The repository is private so I know the OAuth 2 token is valid and being used in the initial clone.

  3. I ONLY have the username and an oauth2 token. I do not have the user's password, SSH key, or personal access token.

Here is my command for cloning:

Git.cloneRepository()
  .setURI(target)
  .setDirectory(repoFolder)
  .setCloneAllBranches(true)
  .setCredentialsProvider(new UsernamePasswordCredentialsProvider("oauth2", token))
  .call();

Here is my command for pushing:

PushCommand push = cloneSource.push();
  push.setRemote(target);
  push.setPushAll();
  push.setCredentialsProvider(new UsernamePasswordCredentialsProvider("oauth2", token));
  push.call();

Upvotes: 5

Views: 4311

Answers (3)

Chandrani H
Chandrani H

Reputation: 156

I am using Git.lsRemoteRepository() method from JGit using OAuth2 access token.

Setting UsernamePasswordCredentialsProvider like this actually worked for me:

    LsRemoteCommand cmd = Git.lsRemoteRepository();
    cmd.setRemote(cloneUrl);
    cmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider("oauth2", gitlab-access-token));

The url is of a private Gitlab repository- https://gitlab.com/my-private-group/testprojectxyxy.git

Upvotes: 0

jugu
jugu

Reputation: 108

For authenticating gitlab using personal access token and jgit, you can use the following snippet:

With the snippet below, I was able to push back to a GitLab repository.

CredentialsProvider credentialsProvider = new 
     UsernamePasswordCredentialsProvider( "PRIVATE-TOKEN", <your oauth token> );
git.push()
     .setCredentialsProvider( credentialsProvider )
     .call();

This works as of Jgit version (4.8.0.201706111038-r)

Upvotes: 6

R&#252;diger Herrmann
R&#252;diger Herrmann

Reputation: 20985

With the snippet below, I was able to clone from and push back to a GitLab repository.

CredentialsProvider credentialsProvider 
  = new UsernamePasswordCredentialsProvider( "myname", "password" );
Git git = Git.cloneRepository()
  .setURI( "https://gitlab.com/myname/repo.git" )
  .setDirectory( tempFolder.newFolder() )
  .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();

If you are using GitLab's Personal Access Tokens, you need to encode the access token into the URL and provide it as a password, like this:

CredentialsProvider credentialsProvider 
  = new UsernamePasswordCredentialsProvider( "myname", "<token>" );
command.setURI( "https://gitlab-ci-token:<token>@gitlab.com/myname/repo.git" )
command.setCredentialsProvider( credentialsProvider )

Maybe that will help to find the source of the 'not authorized' error. In order to communicate through SSH, you need to add an SSH key to your GitLab profile.

Upvotes: 4

Related Questions