无法使用 go-git 和访问令牌 运行 https git 克隆
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
形式的字符串(我的个人 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"
这应该有效:
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")
}
使用 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"
这应该有效:
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")
}