title: Upload SSH public key to bitbucket cloud using curl/REST and token
body: <p>I have a bitbucket cloud account. Under: <a href="https://id.atlassian.com/manage/api-tokens" rel="noreferrer">https://id.atlassian.com/manage/api-tokens</a> I have generated an API Token which I am trying to use in a REST call to upload 
  a public SSH key to my account. Based on:</p>
  
  <p><a href="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&amp;utm_medium=301#idm45427244388592" rel="noreferrer">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&amp;utm_medium=301#idm45427244388592</a></p>
  
  <p><a href="https://community.atlassian.com/t5/Answers-Developer-Questions/Bitbucket-REST-API-POST-using-token-instead-of-basic-auth/qaq-p/474823" rel="noreferrer">https://community.atlassian.com/t5/Answers-Developer-Questions/Bitbucket-REST-API-POST-using-token-instead-of-basic-auth/qaq-p/474823</a></p>
  
  <p>I have tried:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But when I run that I get:</p>
  
  <pre><code>{"type": "error", "error": {"message": "Access token expired. Use your refresh token to obtain a new access token."}}
  </code></pre>
  
  <p>I have tried to re-create the token and re-run the above command again - with the new token - but I get the same error.</p>
  
  <p>Any suggestions?</p>
  
  <p>Based on below answer and link I have now tried:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But I get the exact same error:</p>
  
  <pre><code>{"type": "error", "error": {"message": "Access token expired. Use your refresh token to obtain a new access token."}}
  </code></pre>
  
  <p>So still no luck. Also if I access: </p>
  
  <p><a href="https://api.bitbucket.org/2.0/users/[myuser]/ssh-keys" rel="noreferrer">https://api.bitbucket.org/2.0/users/[myuser]/ssh-keys</a> </p>
  
  <p>directly in a browser I get:</p>
  
  <pre><code>type    "error"
  error   
  message "This API is only accessible with the following authentication types: session, password, apppassword"
  </code></pre>
  
  <p><strong>EDIT/ANSWERED:</strong> Based on the updated answer below I have no tried to create an <strong>app password</strong> and grant it <strong>account: read/write</strong> in bitbucket and it works. I  run it with:</p>
  
  <pre><code>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
  </code></pre>