ChatGPT解决这个技术问题 Extra ChatGPT

如何使用命令行从私有 github 存储库下载单个原始文件?

在 CI 服务器上,我想获取我们在 Github 上维护的配置文件,以便可以在许多作业之间共享。我正在尝试通过 curl 获取此文件,但这些方法都失败了(我得到 404):

# As advised by the oAuth docs
curl -H 'Authorization: token the_token' -L -o setup.sh https://raw.github.com/org/repo/file

# The url of the raw file after clicking to view it
curl -L https://raw.github.com/org/repo/file?login=username&token=the_token 

I
Insung Park

以前的答案不起作用(或不再起作用)。

您可以使用 V3 API 来获取这样的原始文件(您需要一个 OAuth 令牌):

curl -H 'Authorization: token INSERTACCESSTOKENHERE' \
  -H 'Accept: application/vnd.github.v3.raw' \
  -O \
  -L https://api.github.com/repos/owner/repo/contents/path

所有这些都必须在一条线上进行。 -O 选项将文件保存在当前目录中。您可以使用 -o filename 指定不同的文件名。

要获取 OAuth 令牌,请按照此处的说明进行操作:

https://help.github.com/articles/creating-an-access-token-for-command-line-use

我也把它写成一个要点:

https://gist.github.com/madrobby/9476733

编辑:解决方案的 API 参考如下:

https://developer.github.com/v3/#authentication

https://developer.github.com/v3/media/#request-specific-version

https://developer.github.com/v3/repos/contents/#get-contents


请注意,如果文件是公开的,则不需要授权令牌:curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/owner/repo/contents/path 将返回原始文件。
-H 'Accept: application/vnd.github.v3.raw' 是必要的吗?没有该部分,我能够访问私人文件。
@NickChammas:没有该标头,我会得到一个带有元数据的 JSON 响应,以及 base64 编码的实际文件内容,而不是纯文本文件。
请注意,该 URL 与您在浏览器中使用的 URL 不同。我在这里强调了不同之处:https:// api. github.com/ repos/ <owner>/<repo>/ contents/ <path/to/file> (抱歉有点乱)
这也适用于 personal access token。所需的最小权限集是 repoadmin:org/read:org(在私有存储库上)。
t
theartofrain

或者,您可以使用 github“个人访问令牌”(https://github.com/settings/tokens):

TOKEN=...
curl -s https://$TOKEN@raw.githubusercontent.com/<user or organization>/<repo name>/<branch>/<path to file>/<file_name>

例子:

$ curl -s https://1bacnotmyrealtoken123beefbea@raw.githubusercontent.com/concourse/concourse/master/README.md
....

@EM0 - 我刚试过,它奏效了。一些值得仔细检查的事情:1. 主机部分是 raw.githubusercontent.com,2. 路径是 <username>/<repo name>/<branch>/<file name> 3. 令牌需要具有 repo 访问范围。
是的,这就是路径。我从文件的“下载”链接中获取了路径,但从末尾删除了“?token = ...”并添加了令牌。它确实具有 repo 访问范围,但这仅涉及公共存储库。这是一个组织私有存储库。此外,我们启用了 2 因素身份验证,但我认为如果这是问题,它应该给出错误 401,而不是 404。
是的,这一切听起来都不错。路径听起来不错(这是我单击“原始”时得到的路径,去掉了 ?token=... 参数,就像你说的那样)。我的用户也有 2 因素身份验证,我假设我们谈论的是相同的令牌范围(github.com/settings/tokens/new 上的 repo 复选框)。对于它的价值,如果令牌无效,或者没有 repo 范围,您将得到 404(而不是 401)。不知道为什么这在您的设置中不起作用...
奇怪的事情:对我来说使用上面的 curl 命令有效,但是如果我在浏览器中打开相同的链接或尝试通过 java.net.URL.openStream 请求它,我会得到 404...
这是我可以让它在 CMD 中为内部 GitHub 实例工作的唯一方法。对我使用 curl -H 'Authorization: token $TOKEN' $file_url 总是 404。我不确定为什么一个有效而另一个无效,但我从未深入研究 CURL 的文档。
m
mark amos

我知道这是一个老问题,但上面提出的解决方案都不适合我。从那时起,API 可能发生了变化。

这有效:

curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]


这也是唯一对我有用的,但是您的答案标记中有一个小错字。它应该是 [organization]/[repo]/[branch]...
谢谢,只有 Github Enterprise 对我有用。请注意,所需的令牌是个人访问令牌。
@OliverPearmain 你试过 curl -s https://PAT_VALUE@raw.github.company.com/OrgOrUser/RepoName/BranchOrCommitID/file_name.file_extension 吗?这是我唯一可以开始工作的事情,但是公司中的其他人改用了这个解决方案,所以我认为 CURL 可能有某种配置可以减少使用 curl -s... 而不是这个 CLI 集的限制的参数。
P
PeqNP

或者,如果您没有令牌:

curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config

我被要求输入密码,但响应始终是 404。
同样在这里:总是 404
P
Philip Forget

我为此苦苦挣扎了几分钟,直到我意识到所需要的只是将 url 用引号括起来以逃避 & 符号。

curl "https://raw.github.com/org/repo/file?login=username&token=the_token"

这在我的私人回购中对我有用。


J
Jean-Pierre Matsumoto

恕我直言,一个更简单的解决方案是使用 Official GitHub CLI gh

首先您必须登录:

gh auth login

对我来说,这个命令不是必需的,因为我已经登录了。

然后我们需要针对要下载的文件的 API URL。并调用 gh 将其转换为经过身份验证的下载 URL:

API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
curl $(gh api $API_URL --jq .download_url) -o file.ext

一个真实的例子可能更好。这里是从 gh cli 下载 install_linux.md

API_URL=https://api.github.com/repos/cli/cli/contents/docs/install_linux.md
curl $(gh api $API_URL --jq .download_url) -o install_linux.md

API_URL 中:

用户所有者是 cli

存储库名称 repo 也是 cli

文件路径 (path/file.ext) 是 docs/install_linux.md


为避免安装 jq,您可以将其作为选项传递:curl $(gh api $API_URL --jq .download_url) -o file.ext
@BertrandPestre 感谢您的技巧。这是一个非常新的选项:8 天前!
M
Milan Rakos

在浏览器中打开您的 github 存储库:单击文件 在浏览器中打开开发工具:在浏览器中选择网络选项卡 github:单击下载按钮 关闭浏览器开发工具中的弹出窗口:右键单击具有 file_name?token=ABAHQCAT6KG... 的列表选择复制 -> 复制链接地址 url 格式为:https://raw.githubusercontent.com///?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I 在终端:wget -O myFilename https://raw。 githubusercontent.com///?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

链接仅在有限时间内有效,或者您可以创建您的令牌:GitHub article


k
kenorb

当 url 被重定向到 Amazon S3 时,我遇到了身份验证错误:

只允许一种身份验证机制;只有 X-Amz-Algorithm 查询参数...

Authorization: token X 标头更改为 ?access_token=<token> 查询参数对我有用。


A
Abhinav Mishra

我能够让它为 github 企业工作,感谢上面的建议。不得不接受你所有的建议并尝试,最后我能够让它发挥作用。这些是我为使其工作而遵循的步骤。

创建个人令牌,遵循以下步骤:

https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

确保您对令牌具有最低以下权限: repo (Select all under repo) admin:org -> read:org (select "read:org" under "admin:org") 使用以下 curl 命令获取内容:

curl -H "Authorization: token [yourPersonalToken]" -H "Accept: application/vnd.github.v3.raw" -o [filePath]-content.json -L https://github.[company].com/api/v3/repos/[ORG]/[REPO_NAME]/contents/[PATH_TO_FILE]/content.json?ref=[BRANCH_NAME]

哪里->

 [yourPersonalToken] is the token you created.
 [filePath] is a path where you want to save the downloaded copy.
 [company] is the name of company which hosted the github enterprise.
 [ORG] is the github organization is which repo is created.
 [REPO_NAME] is the name of the repository.
 [PATH_TO_FILE] is the path where file is located.
 [BRANCH_NAME] is the name of the branch you want to use, e.g. master, develop etc.

例子:

curl -H "Authorization: token 5a86ecda9ff927baaa66fad2af5bee8" -H "Accept: application/vnd.github.v3.raw" -o C:\Downloads\manifest.json -L https://github.example.com/api/v3/repos/cms/cms_one/contents/app/data/manifest.json?ref=master

从今天开始,此 API 可以下载小于 1 MB 的文件。如果我们需要下载一个大文件,使用这个方法:caludio.medium.com/…
Y
Yevgeniy Brikman

我们不得不经常从私有 GitHub 存储库下载文件,而 hacky shell 脚本并没有完全解决问题,因此我们创建了 fetch,这是一个开源的跨平台工具,可以轻松下载源文件和发布资产来自公共和私有 GitHub 存储库的 git 标记、提交或分支。

例如,要将文件 baz 从私有 GitHub 存储库的版本 0.1.3 下载到 /tmp,您将执行以下操作:

GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --source-path="/baz" /tmp

l
lonewarrior556

只是对已接受答案的补充,如果您使用的是 Github Enterprise url,则略有不同:

curl -H 'Authorization: token [your token]' \
-H 'Accept: application/vnd.github.v3.raw' \
-L https://[your domain]/api/v3/repos/[owner]/[repo-name]/contents/[path of file]

a
atul

令人惊讶的是,在我找到解决方法之前,没有一个答案对我有用。

您可以使用@thomasfuchs 回答的个人访问令牌 https://github.com/settings/tokens

注意:创建令牌时,您必须检查管理员权限。查看相关问题

https://github.com/octokit/octokit.net/issues/1812


管理员提示让它为我工作。否则我刚收到 404。
使其在私人存储库中为我工作的最小权限集是 repoadmin:org/read:org
T
Tony

我认为发行可以访问所有存储库的个人访问令牌(即使只是从我的私人存储库下载单个文件)有点危险且不是好方法。

如何 -

我很乐意推荐对单个文件使用带有令牌的 url。不用担心。令牌字符串将由 github 自动生成。您可以在源代码页面上获取此 url。

通过 curl 或 wget 等转到要下载的源代码页面 找到“原始”按钮并单击它。新页面打开,只需复制 url。此网址如下所示:(https://raw.githubusercontent.com/USERNAME/REPONAME/BRANCHNAME/FILENAME?token=TOKENSTRING)。您可以使用此 url 下载文件


小心,因为令牌附加到该 url will expire after a certain number of days,所以您不能在生产代码中真正使用此解决方案。
此外,似乎每个文件的令牌都不同。
R
Rub
curl -H 'Authorization: token YOUR_TOKEN' \
  -H 'Accept: application/vnd.github.v4.raw' \
  -O \
  -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE

因此,如果原始文件的 url(登录时)是

https://raw.githubusercontent.com/mr_coder/my_repo_name/master/my_script


Then 
  -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
becomes
  -L https://api.github.com/repos/mr_coder/my_repo_name/contents/my_script

注意:我们有 API v4


G
Geoffrey Hudik

对于 GitHub Enterprise 和 API v3,我的 bash 解决方案如下所示(包括 TOKEN 清理/隐私):

TOKEN=yourTokenHere; history -d $((HISTCMD-1)) > /dev/null

curl -H "Authorization: token $TOKEN" \
  -H 'Accept: application/vnd.github.v3.raw' \
  -o file.ext \
  -L http://github.company.com/api/v3/repos/[org]/[repo]/contents/path/file.ext?ref=[branch]

unset TOKEN

P
Pruthviraj Jadhav

我尝试了一个简单的技巧来在 Pycharm 和 Colab 中打开一个 GitHub 私有 .iypnb 文件,它对我来说效果很好。

按 Raw 按钮获取 .ipynb 文件的原始文本,这将打开一些像这样的文本。

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
}]
}

在操作系统(例如 Windows)上打开记事本/文本编辑器将所有文本复制到新的记事本文件中。将记事本保存为 name.ipynb 而不是 name.txt 并保存为文件类型 All Files(.) 而不是 Text Documents (*.txt) 最后在您的 IDE 或 colab 中打开文件。


r
rethab

我从应用程序安装中获得了一个令牌。

以前,您可以使用查询 ?access_token=MY_TOKEN,但那是 deprecated and eventually removed in September 2021

在他们关于 Authenticating with GitHub Apps 的文档中,他们说您可以使用 URL 中的访问令牌和用户名 x-access-token 克隆一个存储库。

这似乎也适用于下载原始文件(ghs_... 是令牌):

$> curl "https://x-access-token:ghs_4qgGKx4skAcaF3bAb3scrTkN4@raw.githubusercontent.com/Octocat/codertocat/main/README.md"

S
Subhakar K S

下面应该可以正常工作。分支名称前的“原始”(在本例中为 master)。

curl -L -O https://github.com/your/repo/raw/master/fetch_file.sh


这个问题是关于私人回购的
P
Prune

您可以使用原始链接执行此操作。

curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file

问题是关于私人回购