我正在尝试使用go-git从GitHub企业版克隆一个存储库。为此,我使用HTTPS协议和一个对我的repos具有适当权限的访问令牌(在命令行上验证)。在进行git-upload-pack RPC调用时,go-git会失败,因为服务器的响应是400:
$ go run main.go
unexpected client error: unexpected requesting "https://github.mycompany.net/my-org/myrepo.git/info/refs?service=git-upload-pack" status code: 400它发出的请求等同于:
GET /my-org/myrepo.git/info/refs?service=git-upload-pack HTTP/1.1
Host: github.mycompany.net
User-Agent: git/1.0
Accept: */*
Authorization: Bearer atokenthatisdefinitelyvalid如果请求头中没有令牌,我将从存储库获得预期的401 (Anonymous access denied)响应。使用该令牌,它将以400作为响应。
我发现对于非企业GitHub上的公共存储库也是如此;不同的是,它(预期)不需要Authorization头,因为没有必要。如果我包含一个有效的令牌,GitHub就像它的企业版一样,以400作为响应。
下面是一个最小的例子。有没有一种方法可以将go-git与需要身份验证的GitHub企业版一起使用?理想情况下使用身份验证令牌?
package main
import (
"fmt"
"io/ioutil"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
const (
repoURL = "https://github.mycompany.net/my-org/myrepo.git"
githubAccessToken = "atokenthatisdefinitelyvalid"
)
func main() {
dir, _ := ioutil.TempDir("", "temp_dir")
options := &git.CloneOptions{
Auth: &http.TokenAuth{Token: githubAccessToken},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}
_, err := git.PlainClone(dir, false, options)
fmt.Println(err)
}发布于 2018-09-07 17:46:55
原来Github使用令牌作为用户的密码,因此它需要基本身份验证,而不是header中的令牌:
options := &git.CloneOptions{
Auth: &http.BasicAuth{Username: "myusername", Token: "mytoken"},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}发布于 2018-08-09 08:03:24
它们能够使用令牌:
https://github.com/src-d/go-git/blob/master/plumbing/transport/http/common.go#L204-L227
import (
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
func main() {
...
auth := http.TokenAuth{Token: "TOKEN_HERE"}
opts := git.CloneOptions{
URL: "https://github.com/user/repo",
Auth: &auth,
}
git.PlainClone("/tmp/cloneDir", false, &opts)
..
}不是100%确定这是否能解决你所有的问题
https://stackoverflow.com/questions/51751828
复制相似问题