git - 使用 go-git 从 GitHub Enterprise 克隆存储库
问题描述
我正在尝试使用go-git
从 GitHub Enterprise 克隆存储库。为此,我使用带有访问令牌的 HTTPS 协议,该访问令牌对我的存储库具有适当的权限(在命令行上验证)。go-git
进行 RPC 调用时失败,git-upload-pack
因为服务器以 400 响应:
$ go run main.go
unexpected client error: unexpected requesting "https://github.mycompany.net/my-org/myrepo.git/info/refs?service=git-upload-pack" status code: 400
它发出的请求等价于:
GET /my-org/myrepo.git/info/refs?service=git-upload-pack HTTP/1.1
Host: github.mycompany.net
User-Agent: git/1.0
Accept: */*
Authorization: Bearer atokenthatisdefinitelyvalid
如果没有请求标头中的令牌,我会从存储库中获得预期的 401 ( Anonymous access denied
) 响应。使用令牌,它会以 400 响应。
我发现非企业 GitHub 上的公共存储库也是如此;不同之处在于它(预期)在没有Authorization
标题的情况下工作,因为没有必要。如果我包含一个有效的令牌,GitHub 就像它的企业版一样以 400 响应。
下面是一个最小的例子。有没有一种方法可以go-git
与需要身份验证的 GitHub Enterprise 一起使用?理想情况下使用身份验证令牌?
package main
import (
"fmt"
"io/ioutil"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
const (
repoURL = "https://github.mycompany.net/my-org/myrepo.git"
githubAccessToken = "atokenthatisdefinitelyvalid"
)
func main() {
dir, _ := ioutil.TempDir("", "temp_dir")
options := &git.CloneOptions{
Auth: &http.TokenAuth{Token: githubAccessToken},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}
_, err := git.PlainClone(dir, false, options)
fmt.Println(err)
}
解决方案
事实证明,Github 使用令牌作为用户的密码,因此它需要基本身份验证而不是标头中的令牌:
options := &git.CloneOptions{
Auth: &http.BasicAuth{Username: "myusername", Token: "mytoken"},
URL: repoURL,
Depth: 500,
ReferenceName: plumbing.ReferenceName("refs/heads/master"),
SingleBranch: true,
Tags: git.NoTags,
}
推荐阅读
- java - 在 Tomcat 服务器上使用 POST 方法从 jsp 到 jsp 的编码错误
- android - 无法在视频上使用 FFmpeg 绘制文本
- javascript - 如何使用我添加的外部脚本来响应 JS?
- elasticsearch - 在批量索引期间检测更改
- regex - 正则表达式逻辑
- node.js - Node js上的Heroku登录问题
- android - TextView 未以编程方式添加到我的布局中
- peoplesoft - 查找 SendMail 人员代码
- json - 用 jq 解析一组 JSON 对象
- typescript - 带有自定义 equals 方法的 TypeScript Set