douzi9744 2015-11-12 07:21
浏览 62

GitHub无法正确解析密钥GoLang API请求

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.

  • 写回答

2条回答 默认 最新

  • dspld86684 2015-11-12 13:10
    关注

    Your OAuth token has no write:public_key scope. Scopes

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题