为什么个人访问令牌不适用于 Bitbucket REST API?

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

TL/DR:(为什么)在 Bitbucket web UI 中创建的 name/PAT 组合不适用于 Bitbucket REST API(或 repo克隆)?

我一直在阅读 various documentation on Bitbucket REST API and Personal Access Tokens,目的是在调用前者时使用后者。

到目前为止,我所读的所有内容都用更详尽的语言表明这是可能的。

接下来,我在 Bitbucket 网站 UI 中创建了一个个人访问令牌 (PAT)。 根据下面的屏幕截图(发布在底部,以免打断文本流),PAT 名称为“test”,假设 PAT 值为 Rzg4MGUyN4m4N9U3O1HQHO3ABJyp7xClvsan7sAmFEPy

为什么,当我尝试使用此 PAT 时,会出现“身份验证失败”错误? 例如:

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"}]}

我的阅读让我相信所有必要的是:

  1. 在 Bitbucket UI
  2. 中创建 PAT
  3. 使用名称/PAT 组合与使用 username/password
  4. 相同

尝试稍微调试一下问题,this page,具体来说,表明 Name/PAT 应该可用于 git clone 回购。但这也失败了:

$ 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/'

任何人都可以给我直接介绍 Bitbucket PATs 以及它们应该如何 configured/used 与 Bitbucket REST API,甚至只是简单的 git cloneing?


the PAT Name is "test",

实际上,您需要使用您的 BitBucket 用户帐户名,而不是 PAT 名称,作为用户名:

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

你也有语法 (seen here)

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

并且您可以使用 gildas/git-credential-bitbucket 来缓存您的令牌并自动重用它

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