因此,下面的代码接收一个公钥,该公钥被创建然后传递给一个对象,然后该对象被编组到json中。然后将此json传递给http请求。 Github正确解析json但返回ssh键无效。如果我复制内容并上网粘贴密钥就可以正常工作。
我确实编辑了密钥以删除密钥的user @ hostname部分(我总是这样做,即使我知道它不安全),看看是不是问题。
^ assert position at start of a line
\d* match a digit [0-9]
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case insensitive)
A-Z a single character in the range between A and Z (case insensitive)
[a-zA-Z\d\s]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case insensitive)
A-Z a single character in the range between A and Z (case insensitive)
\d match a digit [0-9]
\s match any white space character [\r\n\t\f ]
$ assert position at end of a line
这是输出的内容
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)
}
我已经删除了ssh密钥和令牌,所以请不要评论我为什么不发布这些内容。
我知道在请求之前所有内容都经过授权和正确解析(我使用错误的令牌和错误的json格式进行测试都返回了不同的错误)但我不知道为什么这不起作用。我试图只是构建json字符串将其转换为字节数组并传递相同的out。
我不知道这是否重要,但我是通过VPN进行的(我更改了地址以显示github,而不是实际地址)。我已经通过PostMan(不使用VPN)测试了这个调用并且它有效,所以我知道服务器有这些api调用。
答案 0 :(得分:0)
您的OAuth令牌没有write:public_key
范围。 Scopes
答案 1 :(得分:0)
creating a public key的GitHub API文档举例说明了包含小写键的请求正文:
lines = lines.split('\n')
for i, line in enumerate(lines):
line = line.strip().lstrip()
if line.startswith('...') and i != 0:
lines[i - 1] = lines[i - 1].strip().lstrip() + line.replace('...', '')
del lines[i]
,而请求键的第一个字符大写:
{
"title": "octocat@octomac",
"key": "ssh-rsa AAA..."
}
我认为解决这个问题的最简单方法是在{
"Title": "octocat@octomac",
"Key": "ssh-rsa AAA..."
}
结构上使用json struct标记。例如:
githubBody