使用curl / REST和令牌将SSH公钥上传到Bitbucket云

时间:2018-09-28 16:30:02

标签: rest curl bitbucket

我有一个Bitbucket云帐户。在以下位置:https://id.atlassian.com/manage/api-tokens我生成了一个API令牌,我试图在REST调用中使用该令牌来上传 我帐户的公共SSH密钥。基于:

https://docs.atlassian.com/bitbucket-server/rest/5.6.2/bitbucket-ssh-rest.html?utm_source=%2Fstatic%2Frest%2Fbitbucket-server%2F5.6.2%2Fbitbucket-ssh-rest.html&utm_medium=301#idm45427244388592

https://community.atlassian.com/t5/Answers-Developer-Questions/Bitbucket-REST-API-POST-using-token-instead-of-basic-auth/qaq-p/474823

我尝试过:

curl -X POST -d '{"text":"ssh-rsa AAAAB3... me@127.0.0.1"}' -H "Authorization: Bearer ADasdaEeasAsd..." https://bitbucket.org/[my-account]]/rest/ssh/latest/keys

但是当我跑步时,我得到了:

{"type": "error", "error": {"message": "Access token expired. Use your refresh token to obtain a new access token."}}

我尝试重新创建令牌,并使用新令牌再次运行上述命令-但我遇到相同的错误。

有什么建议吗?

基于下面的答案和链接,我现在已经尝试过:

curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer wxdrtblabla..." \
-d '{"key": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKqP3Cr632C2dNhhgKVcon4ldUSAeKiku2yP9O9/bDtY myuser@bitbucket.org/myuser"}' \
https://api.bitbucket.org/2.0/users/myuser/ssh-keys

但是我得到了完全相同的错误:

{"type": "error", "error": {"message": "Access token expired. Use your refresh token to obtain a new access token."}}

所以仍然没有运气。另外,如果我访问:

https://api.bitbucket.org/2.0/users/[myuser]/ssh-keys

直接在浏览器中我得到:

type    "error"
error   
message "This API is only accessible with the following authentication types: session, password, apppassword"

编辑/答复::根据下面的更新答案,我没有尝试创建应用密码并授予其帐户:读/写在bitbucket中运行。我用它来运行它:

curl -v -u myuser:my-generated-app-password -X POST  \
-H "Content-Type: application/json" \
-d '{"key": "ssh-rsa AAA....ro"}' \
https://api.bitbucket.org/2.0/users/myuser/ssh-keys

1 个答案:

答案 0 :(得分:5)

您正在查看Bitbucket Server文档,但使用的是Bitbucket Cloud。 (赠品:doc路径的“ bitbucket-server”部分,以及您推动密钥的路径中的“ bitbucket.org”。)

请改用https://developer.atlassian.com/bitbucket/api/2/reference/resource/users/%7Busername%7D/ssh-keys#post-这是Bitbucket Cloud文档来完成您要尝试的操作。您的网址将更像https://api.bitbucket.org/2.0/users/[your-account]/ssh-keys

编辑:收到的错误表明存在问题:您需要从现有会话(即从GUI)进行该呼叫,使用密码或使用app password。我建议您使用该应用程序的密码,因为它的范围很广,可以一次性使用,并且不会让您登录GUI。然后,您的curl调用将变成类似curl -u myuser:myapppassword -X POST -H "Content-Type: application/json" -d '{"key": "key content goes here"}' https://api.bitbucket.org/2.0/users/myuser/ssh-keys的地方。