Use JGit to perform clone, add, commit and push Git operations with Bitbucket access tokens.
I’m using Eclipse for development with Bitbucket Access Tokens. I found that the 2022-03 version of Eclipse was able to clone Bitbucket repositories using Access Tokens. I found that the ‘Clone URI’ operation using Bitbucket Access Tokens did NOT work for the following versions of Eclipse: 2024-09, 2024-12, 2025-12, 2026-03. I was also unable to use the Bitbucket ‘git clone’ command provided when an access token was created in the Windows Command Prompt interface:
The response message was:
I performed the following searches on Chrome:
The first search initially gave the following response (but it did not respond the same way with subsequent tries):
The two JGIT searches provided sample code which I packaged into a Java Swing interface application which performs all the basic operations. The code below shows the code that implements these operations. I hope that some may find this useful.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code Start
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.bitbucket;
import java.io.File;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class BitbucketMessage {
public void parseUriCloneAddCommitPush() throws Exception {
File localRepositoryDirectory = null;
Git git = null;
String bitbucketAddress = null;
String commitMessage = null;
String folderNameLc = null;
String httpHeader = null;
String localRepositoryLocation = null;
String remoteUri = null;
String token = null;
String uri = null;
String workspaceFolderPath = null;
String workspaceName = null;
String xTokenAuth = null;
String[] arrayAtSign = null;
String[] arrayColon = null;
String[] arrayDoubleSlash = null;
try {
//-----------
// Parse URI
//-----------
//Set workspace (folder) name
workspaceName = "WorkspaceName";
//Set local repository location
workspaceFolderPath = "C:/.../.../.../" + workspaceName;
//Convert to lower case
folderNameLc = workspaceName.toLowerCase();
//Set local repository location
localRepositoryLocation = workspaceFolderPath + "/" + folderNameLc;
//Get uri
uri = "git clone https://x-token-auth:<TOKEN>@bitbucket.org/username/repositoryLowerCase.git";
//Get segment arrays
arrayDoubleSlash = uri.split("://", -1);
arrayColon = arrayDoubleSlash[1].split(":", -1);
arrayAtSign = arrayColon[1].split("@", -1);
//Get base values
httpHeader = arrayDoubleSlash[0].replace("git clone", "").trim();
xTokenAuth = arrayColon[0].trim();
token = arrayAtSign[0].trim();
bitbucketAddress = arrayAtSign[1].trim();
//Prepare values for clone operation
httpHeader = httpHeader + "://";
xTokenAuth = xTokenAuth + ":";
bitbucketAddress = "@" + bitbucketAddress;
//Construct remote uri
remoteUri = httpHeader + xTokenAuth + token + bitbucketAddress;
//-----------
// Clone
//-----------
//Clone repository
Git.cloneRepository()
.setURI(remoteUri)
.setDirectory(new File(localRepositoryLocation))
.call();
//-----------
// Add, Commit, Push
//-----------
//Create folder instance
localRepositoryDirectory = new File(localRepositoryLocation);
//Open repository
git = Git.open(localRepositoryDirectory);
//Perform 'git add .' to stage all new/modified files
git.add()
.addFilepattern(".")
.call();
//Perform 'git commit' locally
git.commit()
.setMessage(commitMessage)
.call();
//Chain this to your local 'git' object initialized above
git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("x-token-auth", token))
.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code End
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Community moderators have prevented the ability to post new answers.
Hi Martin,
Thanks for sharing the JGit example. The main thing to keep in mind is that Bitbucket Cloud has different token types, and the authentication format depends on which one you created.
For a Bitbucket repository/project/workspace access token, x-token-auth is the correct username when using HTTPS, for example:
https://x-token-auth:<ACCESS_TOKEN>@bitbucket.org/<workspace>/<repository>.gitAtlassian documents this format for cloning, and the token needs the appropriate repository permissions such as Repository Read for cloning and Repository Write for pushing.
For a normal Bitbucket API token, however, the authentication format is different. Atlassian documents x-bitbucket-api-token-auth (or the account's Bitbucket username) as the username, with the API token supplied as the password.
So one possible reason the command shown in the post fails is that the token type and authentication format do not match.
For JGit, your approach of using:
new UsernamePasswordCredentialsProvider("x-token-auth", token)is appropriate when the credential is specifically a Bitbucket access token. The important part is to make sure the token has the required repository permissions and that the repository/workspace path is correct.
One other point: embedding the token directly into the remote URL works, but Atlassian recommends avoiding storing access tokens permanently in URLs because they can be exposed in Git configuration or logs. Using a credentials provider/secure secret is preferable.
So the JGit solution itself is valid; the key is matching the token type, username format, scopes/permissions, and repository URL correctly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.