为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用 go-git 和访问令牌运行 https git clone

无法使用 go-git 和访问令牌运行 https git clone

Go
翻过高山走不出你 2022-10-17 16:47:07
使用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"
查看完整描述

1 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

这应该有效:


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")

}


查看完整回答
反对 回复 2022-10-17
  • 1 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号