So the following code recieves a public key that is created then passed into an object that is then marshalled into json. This json is then passed into an http request. Github is parsing the json correctly but returns that the ssh key is invalid. If I copy the contents and go online to paste the key it works fine.
I did edit the key to remove the user@hostname part of the key (I always do even though I know it is unsafe) to see if that was the problem.
func addKeyToGitHub(token string, comment string, publickey []byte) (*http.Response, error) {
if token == "" {
fmt.Println("Please create a token that has 'write:public_key' scope")
open.Run(githubAPPURL)
ir := bufio.NewReader(os.Stdin)
fmt.Print("Enter Token: ")
token, _ = ir.ReadString('
')
}
k := string(publickey)
//Removes unwanted host at end of file
array := strings.Split(k, " ")
array = array[:len(array)-1]
k = strings.Join(array, " ")
fmt.Println(k)
b := &githubBody{Title: comment, Key: k}
body, err := json.Marshal(b)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", githubAPIURL+"user/keys", bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Content-Type", "application/json")
fmt.Println(req)
client := http.Client{}
return client.Do(req)
}
Here is what it outputs
Please create a token that has 'write:public_key' scope
Enter Token: 3310b4ef5d0dbbb8687b992e6f78e02cd34e4d6d
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7zwo/h3mVL2RwOyMeUyGZblLT+nZy9pS6akUMSiOaVkphCdQzoHH/6xbDPiTKNlXC+1JYmQq4DaeglFdVc+FQHp3ZlbhZp6YPemXHnGtKamtQrJHS+HA7zE+F3xPzLQlVTgUdlE4ckU0THRK7WdwXa1sSMO2UNBrn7MGZ4wskU4PO8QFBjFHN0e5SIv2mlF/Oi1FoSRHIEohHAY6JSE8lnxkYFA5XeVkygLFQbZ7dKpVaZg4Y1LvrZUnThmI+e1QxP9ttUj5Ike6wFewBTvTjtPb8hkEHp3GuwjmwN9SKIW4d5bmrJtGxQjYmY4PHhW5DnySBQWBeOnQxhqVQFqlN
&{POST http://github.com/api/v3/user/keys HTTP/1.1 1 1 map[Authorization:[token 3310b4ef5d0dbbb8687b992e6f78e02cd34e4d6d] Content-Type:[application/json]] {{"Key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7zwo/h3mVL2RwOyMeUyGZblLT+nZy9pS6akUMSiOaVkphCdQzoHH/6xbDPiTKNlXC+1JYmQq4DaeglFdVc+FQHp3ZlbhZp6YPemXHnGtKamtQrJHS+HA7zE+F3xPzLQlVTgUdlE4ckU0THRK7WdwXa1sSMO2UNBrn7MGZ4wskU4PO8QFBjFHN0e5SIv2mlF/Oi1FoSRHIEohHAY6JSE8lnxkYFA5XeVkygLFQbZ7dKpVaZg4Y1LvrZUnThmI+e1QxP9ttUj5Ike6wFewBTvTjtPb8hkEHp3GuwjmwN9SKIW4d5bmrJtGxQjYmY4PHhW5DnySBQWBeOnQxhqVQFqlN","Title":"dixon@homedesktop"}} 418 [] false github.com map[] map[] <nil> map[] <nil> <nil>}
Status 422
Body: {"message":"Validation Failed","documentation_url":"https://developer.github.com/v3/users/keys/#create-a-public-key","errors":[{"resource":"PublicKey","code":"custom","field":"key","message":"key is invalid. It must begin with 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', or 'ecdsa-sha2-nistp521'. Check that you're copying the public half of the key"}]}
I have deleted the ssh key and token so please no comments on why I should not post those.
I know everything is authorized and parsed correctly before the request (I have tested with wrong token and wrong json format both return a different error) but I do not know why this is not working. I have tried to just build the json string turn it into a byte array and pass that with the same out come.
I don't know if this matters but I am doing this through a VPN (I changed the address to show github and not the actual address). I have tested this call through PostMan (not using VPN) and it worked, so I know the server has these api calls.