无法使用go-git和访问令牌运行https git克隆。

huangapple go评论105阅读模式
英文:

Unable to run https git clone using go-git and access token

问题

使用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的字符串(我的个人GitHub访问令牌),

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范围。

编辑:

fs的实例化如下:

fs := memfs.New()

使用的http包是:

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

Using go-git/v5 and trying to clone over https as follows:

	_, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
		URL:           repo,
		ReferenceName: plumbing.ReferenceName(branch),
		Depth:         1,
		SingleBranch:  true,
		Auth:          &http.TokenAuth{Token: string(token)},
	})

where token is a string of the form ghp_XXXXXXXXX (my personal GH access token)

and repo equals to my private repo https://github.com/pkaramol/arepo

The error is

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

I have also trying using basic auth with my username and the token as password

	_, 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},
	})

Now the error becomes:

authentication required

What is the proper way of cloning over https?

The token has repo scope fwiw

edit:

the fs is instantiated as follows

fs := memfs.New()

the http package used is the following

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

答案1

得分: 1

这应该可以工作:

package main

import (
	"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")
}
英文:

This should work:

package main

import (
	"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")
}

huangapple
  • 本文由 发表于 2021年12月4日 21:00:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70225815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定