在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
答案 0 :(得分:63)
以前的答案不起作用(或不再起作用)。
您可以使用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参考如下:
答案 1 :(得分:14)
或者,您可以使用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
....
答案 2 :(得分:8)
我知道这是一个老问题,但上面提出的解决方案都不适合我。从那时起,API可能已经发生了变化。
这有效:
curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]
答案 3 :(得分:6)
或者,如果您没有令牌:
curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
答案 4 :(得分:4)
我正在为此奋斗几分钟,直到我意识到所需要的只是将引号中的url包裹起来以逃避&符号。
curl "https://raw.github.com/org/repo/file?login=username&token=the_token"
这在我的私人仓库中对我有用。
答案 5 :(得分:2)
当网址重定向到Amazon S3时,我遇到了身份验证错误:
只允许一种身份验证机制;只有
X-Amz-Algorithm
查询参数...
从Authorization: token X
标题更改为?access_token=<token>
查询参数为我工作。
答案 6 :(得分:1)
我们不得不经常从私有GitHub repos下载文件而hacky shell脚本并没有完全削减它,因此我们创建了fetch,这是一个开源的跨平台工具,可以轻松下载源文件和发布资产来自git标记,提交或公共和私有GitHub存储库的分支。
例如,要将文件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
答案 7 :(得分:1)
我认为颁发个人访问令牌有点危险,不是好方法,即使只是从我的私人存储库下载一个文件,它也可以访问所有存储库。
我很乐意推荐使用 url 和单个文件的令牌。别担心。令牌字符串将由 github 自动生成。您可以在源代码页上获取此网址。
答案 8 :(得分:1)
令人惊讶的是,直到找到解决方法,所有答案都没有对我有用。
您可以使用@thomasfuchs回答的个人访问令牌https://github.com/settings/tokens
注意:创建令牌时,必须检查管理员权限。请参阅相关问题
答案 9 :(得分:0)
我尝试了一个简单的技巧,在 Pycharm 和 Colab 中打开一个 GitHub 私有 .iypnb 文件,它对我来说效果很好。
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
}]
}
在操作系统(例如 Windows)上打开记事本/文本编辑器,将所有文本复制到一个新的记事本文件中。
将记事本保存为 name.ipynb 而不是 name.txt 并保存为文件类型 All Files(.) 而不是 Text Documents (*.txt)
最后在您的 IDE 或 colab 中打开文件。
答案 10 :(得分:0)
我能够让它为 github 企业工作,感谢上面的建议。不得不接受你所有的建议并尝试,最后我能够让它发挥作用。这些是我为使其工作而遵循的步骤。
https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
确保您对令牌具有以下最低权限:
使用以下 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
答案 11 :(得分:0)
对于GitHub Enterprise和API v3,我的bash解决方案如下所示(包括令牌清除/隐私):
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
答案 12 :(得分: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
因此,如果原始文件的网址(登录时)是
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
答案 13 :(得分:0)
file_name?token=ABAHQCAT6KG...
选择副本->复制链接地址
网址的格式为:
https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
在终端:
wget -O myFilename https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
链接仅在有限的时间内有效,或者您可以创建令牌:GitHub article
答案 14 :(得分:0)
只是接受的答案的补充,如果您使用的是Github Enterprise网址,则略有不同:
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]
答案 15 :(得分:0)
下面应该可以正常工作。分支名称前的“原始”(本例中为master)。
position: relative;
答案 16 :(得分:-4)
您可以使用原始链接执行此操作。
curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file