Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JGit to push changes to remote with OAuth access token

Tags:

java

jgit

I am able to push to remote with this piece of code

return git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).setRemote("origin").call();

I also am able to get oauth access token with all necessary scopes. How do I push with access token?

like image 256
user1745356 Avatar asked Jan 21 '15 17:01

user1745356


People also ask

How do I update my personal access tokens?

Updating an existing Access Token If your existing token has expired, or been revoked, or you are on a new machine and do not have access to the existing token then you can regerate a new one in the Github console Settings -> Developer settings -> Personal access tokens .


1 Answers

You need to pass a UsernamePasswordCredentialsProvider with the token as its user name.

String remoteUrl = "https://${token}@github.com/user/repo.git";
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider("${token}", "");
git.push().setRemote(remoteUrl).setCredentialsProvider(credentialsProvider).call();

The above can at least be used to authenticate with a GitHub repository. Though, I can't say if this scheme works generally.

like image 61
Rüdiger Herrmann Avatar answered Sep 22 '22 15:09

Rüdiger Herrmann