2

I am trying as follows:

SSHKEY=`cat ~/.ssh/id_rsa.pub`
curl -u username:password -X POST -H "Content-Type: application/json" -d '{"key": "$SSHKEY", "label": "someLabel"}' https://api.bitbucket.org/2.0/users/username/ssh-keys

But it is giving:

{"type": "error", "error": {"fields": {"key": ["That SSH key is invalid."]}, "message": "key: That SSH key is invalid."}}

Any idea on how to send this ssh key to bitbucket using api?

Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19

1 Answers1

1

Ok, I made it work using a python 3 script.

#!/usr/bin/python3
import os
import requests, json

url = "https://api.bitbucket.org/2.0/users/userName/ssh-keys"

key = open(os.path.expanduser('~/.ssh/id_rsa.pub')).read()
print(key)

payload = {
        "key": key, 
        "label": "testSSHKey"
       }

header = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} 

response_decoded_json = requests.post(url, auth=requests.auth.HTTPBasicAuth('userName', 'password'), data=payload, headers=header)
response_json = response_decoded_json.json()

print(response_json)
Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19