使用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"
uj5u.com熱心網友回復:
這應該有效:
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")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373109.html
上一篇:在GoLang中取消背景關系時如何在函式中立即回傳?
下一篇:無法修改函式中的矩陣
