所以下面的代码接收一个创建的公钥,然后传递给一个对象,然后编组到 json 中。然后将此 json 传递到 http 请求中。Github 正在正确解析 json,但返回 ssh 密钥无效。如果我复制内容并在线粘贴密钥,则它可以正常工作。
我确实编辑了密钥以删除密钥的 user@hostname 部分(即使我知道它不安全,我也总是这样做)以查看是否是问题所在。
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('\n')
}
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)
}
这是它输出的内容
Please create a token that has 'write:public_key' scope
Enter Token: 3310b4ef5d0dbbb8687b992e6f78e02cd34e4d6d
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"}]}
我已经删除了 ssh 密钥和令牌,所以请不要评论为什么我不应该发布这些。
我知道在请求之前一切都已被授权并正确解析(我已经用错误的令牌和错误的 json 格式进行了测试,它们都返回了不同的错误)但我不知道为什么这不起作用。我试图只构建 json 字符串,将其转换为一个字节数组,并以相同的输出来传递它。
我不知道这是否重要,但我是通过 VPN 进行的(我更改了地址以显示 github 而不是实际地址)。我已经通过 PostMan(不使用 VPN)测试了这个调用并且它有效,所以我知道服务器有这些 api 调用。
qq_笑_17
月关宝盒
相关分类