猿问

无法使用 go-git 和访问令牌运行 https git clone

使用go-git/v5并尝试克隆https如下:


    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{

        URL:           repo,

        ReferenceName: plumbing.ReferenceName(branch),

        Depth:         1,

        SingleBranch:  true,

        Auth:          &http.TokenAuth{Token: string(token)},

    })


token表单的字符串在哪里ghp_XXXXXXXXX(我的个人 GH 访问令牌)


repo等于我的私人回购https://github.com/pkaramol/arepo


错误是


"net/http: invalid header field value \"Bearer ghp_XXXXXXXXX`\\n\" for key Authorization"

我还尝试将基本身份验证与我的用户名和令牌作为密码一起使用


    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{

        URL:           repo,

        ReferenceName: plumbing.ReferenceName(branch),

        Depth:         1,

        SingleBranch:  true,

        Auth:          &http.BasicAuth{Username: "pkaramol", Password: token},

    })

现在错误变为:


authentication required

通过https克隆的正确方法是什么?


该令牌具有repo范围 fwiw


编辑:


实例化fs如下


fs := memfs.New()

使用的http包如下


"github.com/go-git/go-git/v5/plumbing/transport/http"


翻过高山走不出你
浏览 146回答 1
1回答

互换的青春

这应该有效:package mainimport (    "os"    "fmt"    "github.com/go-git/go-billy/v5/memfs"    "github.com/go-git/go-git/v5/plumbing"    "github.com/go-git/go-git/v5/plumbing/transport/http"    "github.com/go-git/go-git/v5/storage/memory"    git "github.com/go-git/go-git/v5")func main() {    token := "ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"    fs := memfs.New()    _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{        URL:           "https://github.com/username/reponame",        ReferenceName: plumbing.ReferenceName("refs/heads/main"),        Depth:         1,        SingleBranch:  true,        Auth:          &http.BasicAuth{Username: "username", Password: token},        Progress:      os.Stdout,    })    if err != nil {        fmt.Println(err)    }    fmt.Println("Done")}
随时随地看视频慕课网APP

相关分类

Go
我要回答