Search code examples
bitbucketaccess-tokengit-clonebitbucket-api

Why is Personal Access Token not working for Bitbucket REST API?


TL/DR: (Why) does a name/PAT combination created in the Bitbucket web UI not work with Bitbucket REST API (or repo cloning)?

I've been reading various documentation on Bitbucket REST API and Personal Access Tokens, with the intent of using the latter when invoking the former.

All that I have read so far have said -- in more elaborative words -- that this is possible.

Following along, I created a Personal Access Token (PAT) in the Bitbucket web UI. Per the screenshot below (posted at the bottom, so as to not break up the text flow), the PAT Name is "test", and let's say the PAT value is Rzg4MGUyN4m4N9U3O1HQHO3ABJyp7xClvsan7sAmFEPy

Why, when I try to use this PAT, do I get an "Authentication failed" error? For example:

user@bld_svr:~$ curl -L -u test:Rzg4MGUyN4m4N9U3O1HQHO3ABJyp7xClvsan7sAmFEPy -H "Content-Type: application/json" -X POST https://bitbucket.svr.com/rest/build-status/1.0/commits/5764bb32f80813b3bbcbf6496c113c1830c52bb0 -d '{"state":"INPROGRESS","key":"job_434","url":"http://bld_svr:8080//blue/organizations/jenkins/prj/detail/prj/434/pipeline"}'
{"errors":[{"context":null,"message":"Authentication failed. Please check your credentials and try again.","exceptionName":"com.atlassian.bitbucket.auth.IncorrectPasswordAuthenticationException"}]}

My readings have led me to believe that all that's necessary is:

  1. Creating the PAT in the Bitbucket UI
  2. Using the Name/PAT combination the same as one would use username/password

Trying to debug the issue a little, this page, specifically, indicates that Name/PAT should be usable to git clone repos. But that also fails:

$ git clone https://bitbucket.svr.com/scm/key/a_project.git
Cloning into 'a_project'...
Username for 'https://bitbucket.svr.com': test
Password for 'https://test@bitbucket.svr.com': # I typed in "Rzg4MGUyN4m4N9U3O1HQHO3ABJyp7xClvsan7sAmFEPy" here
fatal: Authentication failed for 'https://bitbucket.svr.com/scm/key/a_project.git/'

Can anyone give me the straight dope on the Bitbucket PATs and how they should be configured/used with Bitbucket REST API, and even just plain git cloneing?


enter image description here


Solution

  • the PAT Name is "test",

    Actually, you would need to use your BitBucket user account name, not the PAT name, as username:

    git clone https://username:<token>@bitbucketserver.com/scm/projectname/teamsinspace.git
    

    You also have the syntax (seen here)

    git clone "https://x-token-auth:{tokenHere}@bitbucket.org/yourRepoOwnerHere/RepoNameHere"
    

    And you can use gildas/git-credential-bitbucket as a way to cache your token and reuse it automatically

    git credential-bitbucket store <<EOM
    protocol=https
    host=bitbucket.org
    username=xxx
    clientid=yyy
    secret=zzz
    
    git remote add bitbucket https://xxx@bitbucket.org/path/to/repo.git
    git config credential.helper bitbucket