首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用go-git和访问令牌运行https git克隆。

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

Stack Overflow用户
提问于 2021-12-04 13:00:19
回答 1查看 54关注 0票数 0

使用go-git/v5并尝试对https进行克隆,如下所示:

代码语言:javascript
复制
    _, 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

错误是

代码语言:javascript
复制
"net/http: invalid header field value \"Bearer ghp_XXXXXXXXX`\\n\" for key Authorization"

我也尝试用我的用户名和令牌作为密码使用basic auth。

代码语言:javascript
复制
    _, 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},
    })

现在,错误变成:

代码语言:javascript
复制
authentication required

在https上克隆的正确方法是什么?

令牌具有repo作用域fwiw。

编辑:

fs实例化如下所示

代码语言:javascript
复制
fs := memfs.New()

使用的http包如下

代码语言:javascript
复制
"github.com/go-git/go-git/v5/plumbing/transport/http"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-04 13:59:19

这应该是可行的:

代码语言:javascript
复制
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")
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70225815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档