【问题标题】:Unable to run https git clone using go-git and access token无法使用 go-git 和访问令牌运行 https git clone
【发布时间】:2022-01-10 12:01:04
【问题描述】:

使用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"

【问题讨论】:

  • 您的 BasicAuth 示例对我来说很好。你能用你正在使用的和周围代码的确切导入来编辑你的帖子吗(例如fs 实例化)?
  • 添加了一些更新,如果还不够请告诉我
  • 是的,我们是一致的,它对我有用。我猜你的令牌有问题。我假设您尝试创建一个新令牌。如何实例化令牌?我只是做token := "ghp_XXXXXXXXXXXXXXXXXXXXXXX"
  • 我会发布我的代码作为答案,以便您进行比较

标签: git go go-git


【解决方案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")
}

【讨论】:

  • duh....我的令牌变量中有一个 \n 字符(我正在从文件中读取它)。感谢您回答问题,我接受您的回答
猜你喜欢
  • 2021-09-01
  • 2022-07-03
  • 2020-01-04
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
相关资源
最近更新 更多