英文:
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")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论