97

在 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 
4

18 回答 18

137

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

您可以使用 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 令牌,请按照此处的说明进行操作:

我也把它写成一个要点:

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

于 2014-03-11T00:42:45.213 回答
36

或者,您可以使用 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
....
于 2016-08-17T22:37:31.837 回答
16

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

这有效:

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

于 2016-02-27T02:22:09.297 回答
7

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

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

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

于 2013-08-20T17:19:39.627 回答
7

或者,如果您没有令牌:

curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
于 2013-11-08T00:12:50.480 回答
3
  1. 在浏览器中打开您的 github 存储库:单击文件
  2. 在浏览器中打开开发者工具:选择网络选项卡
  3. 在浏览器 github 中:点击下载按钮
  4. 关闭弹出窗口
  5. 在浏览器开发工具中:右键单击具有 file_name?token=ABAHQCAT6KG...
  6. 选择复制->复制链接地址

    网址格式为:

    https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

  7. 在终端:

    wget -O myFilename https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

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

于 2019-05-15T08:55:11.517 回答
2

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

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

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

于 2014-12-11T06:23:21.943 回答
2

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

  1. 首先您必须登录:
gh auth login

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

  1. 然后我们需要针对要下载的文件的 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

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

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

  • 用户ownercli
  • 存储库名称repocli
  • 文件路径 ( path/file.ext) 是docs/install_linux.md
于 2020-12-25T23:15:29.667 回答
1

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

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

GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --source-path="/baz" /tmp
于 2016-06-20T16:38:34.077 回答
1

只是对已接受答案的补充,如果您使用的是 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]
于 2018-08-28T11:07:49.197 回答
1

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

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

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

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

于 2020-01-03T12:08:44.780 回答
1

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

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

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

  1. 确保您对令牌具有最低以下权限:

    • 回购(在回购下全选)
    • admin:org -> read:org(在“admin:org”下选择“read:org”) 在此处输入图像描述
  2. 使用以下 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
于 2021-03-04T14:37:44.343 回答
0
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

于 2020-03-29T18:34:14.190 回答
0

对于 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
于 2020-07-06T21:25:24.393 回答
0

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

如何 -

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

  1. 通过 curl 或 wget 等转到要下载的源代码页面
  2. 找到“原始”按钮并单击它。 在此处输入图像描述
  3. 新页面打开,只需复制 url。此网址如下所示:(
    https://raw.githubusercontent.com/USERNAME/REPONAME/BRANCHNAME/FILENAME?token=TOKENSTRING
  4. 您可以使用此 url 下载文件
于 2020-12-19T07:12:40.047 回答
0

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

  1. 按 Raw 按钮获取 .ipynb 文件的原始文本,这将打开一些像这样的文本。
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
}]
}
  1. 在操作系统(例如 Windows)上打开记事本/文本编辑器将所有文本复制到新的记事本文件中。

  2. 将记事本保存为 name.ipynb 而不是 name.txt 并将保存为文件类型 All Files( . ) 而不是 Text Documents (*.txt)

  3. 最后在您的 IDE 或 colab 中打开文件。

于 2021-05-30T19:01:50.523 回答
-3

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

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

于 2017-02-01T19:51:58.870 回答
-5

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

curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file
于 2016-05-20T18:32:45.030 回答