1

I'm trying to send my public ssh key to Github. I set my key to a variable in shell but it gives key is invalid. You must supply a key in OpenSSH public key format error. I have authentication to send key but it doesn't work.

Lines that star with ➜ ~ are commands and rest are output.

➜  ~ key=$(cat .ssh/id_0001.pub)
➜  ~ echo $key 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDCk9BFg0PyDeOn186G+gx16KjeYPekGlPppNcg1shjqzVSppNJHXy08GqBNgxN14TU3UzQyRej6BjSi0YGlWVZAz1tDedaFsauwCYYRIO9Aej7HJcmU9V8Sv9npWBGjQZTS/KYRg790xo8zueoCzCXR+Sufxy6yn5xee+e938nkdY0O+/Lxv/3Ks6IQpvJlCOYdvvUC1liIQWd2nxc9T3vtjWtslG5/Yq4PzBT4ja5A0xMNerCrIXf7e7m+DtV2jIESYCwYMBOAioRdSDHIGHL4YOoikYeebUFe1ajTalZYEmsFW/0yu6GEhvoe9FP1IhPIN5nPI6h3EJWDDEpOOI6IdG4Ex5Slzg+TRYG6CoUhRb1d/iQrcSzdibP450GrqxaSMZicmOEj/7w6SvItd1qJ+my3W1RZmGhRQ8CaXMpZvJERFdS2tAmO42r3IC24Xe7ZU+vvlxgS+H7F19kAWWhL8Q8OGFmC9Zs5gkci/jxG6f7Wlnd35UHJ9VreCQl/t0= archie@PC 
➜  ~ curl -H "Authorization: token 90********************" --data '{"title":"test-key","key":"${key[@]}"}' https://api.github.com/user/keys 
{ 
  "message": "Validation Failed", 
  "errors": [ 
    { 
      "resource": "PublicKey", 
      "code": "custom", 
      "field": "key", 
      "message": "key is invalid. You must supply a key in OpenSSH public key format" 
    } 
  ], 
  "documentation_url": "https://docs.github.com/rest/reference/users#create-a-public-ssh-key-for-the-authenticated-user" 
} 

2 Answers2

0

This is for Windows users, there are two things to consider

  1. your console should parse quotation and double quotation in the right order.
  2. curl needs quotation for JSON properties and keys.

So how can we debug it:

  • Add "-v --trace-ascii -" to the curl command to see log for command.
  • Use echo or write-host for your data in cmd or PowerShell

Example for Windows PowerShell

$publicKey =Get-Content -Path "$HOME\.ssh\github.pub" 

$data='{""title"":""script"",""key"":""' + $publicKey + '""}'

curl -X POST -H "Accept: application/vnd.github.v3+json" -H "Authorization: token $userToken"  https://api.github.com/user/keys  -d $data -v --trace-ascii -

It is so important to make sure that curl receives valid JSON data.

Bashir Momen
  • 1,461
  • 19
  • 17
-2

I meet the same problem. I think you not correctly to generate a new SSH key. After you typing ssh-keygen -t rsa -C "your email@163.com", you will see that in follow.And you should use clip < ~/.ssh/id_rsa.pub to copy the keys.

s0xzwasd
  • 2,895
  • 3
  • 17
  • 26