Curl 如何使用命令行从私有github repo下载单个原始文件?
在CI服务器上,我想获取我们在Github上维护的配置文件,以便它可以在多个作业之间共享。我试图通过curl获取此文件,但这两种方法都失败了(我得到了404):Curl 如何使用命令行从私有github repo下载单个原始文件?,curl,github,oauth,Curl,Github,Oauth,在CI服务器上,我想获取我们在Github上维护的配置文件,以便它可以在多个作业之间共享。我试图通过curl获取此文件,但这两种方法都失败了(我得到了404): 我为此挣扎了几分钟,直到我意识到所需要的只是将url用引号括起来,以避开符号 curl "https://raw.github.com/org/repo/file?login=username&token=the_token" 这在我的私人回购中对我有效。或者,如果你没有代币: curl --user [your_user]
我为此挣扎了几分钟,直到我意识到所需要的只是将url用引号括起来,以避开符号
curl "https://raw.github.com/org/repo/file?login=username&token=the_token"
这在我的私人回购中对我有效。或者,如果你没有代币:
curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
前面的答案不起作用(或者不再起作用) 您可以使用V3API获得如下原始文件(您需要OAuth令牌):
curl-H'Authorization:token INSERTACCESSTOKENHERE'\
-H'Accept:application/vnd.github.v3.raw'\
-O\
-Lhttps://api.github.com/repos/owner/repo/contents/path
所有这些都必须在一条线上完成。-O
选项将文件保存在当前目录中。您可以使用-o filename
指定不同的文件名
要获取OAuth令牌,请按照以下说明操作:
X-Amz-Algorithm
查询参数
从
Authorization:token X
头更改为?access\u token=
query参数对我有效。我知道这是一个老问题,但上面提出的任何解决方案对我都不起作用。也许从那时起API已经改变了
这起到了作用:
curl-H'Authorization:token[在此处插入您的令牌]”-o output.txthttps://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]
您可以通过原始链接完成此操作
curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file
我们不得不经常从私有GitHub repos下载文件,而黑客的shell脚本并不能很好地解决这个问题,因此我们创建了一个开源的跨平台工具,它可以轻松地从git标记、提交或公共和私有GitHub repos的分支下载源文件并释放资产 例如,要将文件
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
或者,您可以使用github“个人访问令牌”():
下面应该可以。分支名称前有一个“raw”(在本例中为master)
curl-L-Ohttps://github.com/your/repo/raw/master/fetch_file.sh
如果您使用的是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]
在浏览器中打开github repo:单击文件
在浏览器中打开开发人员工具:选择“网络”选项卡
在浏览器github中:单击下载按钮
关闭弹出窗口
在浏览器开发工具中:右键单击具有
file\u name?token=ABAHQCAT6KG…
选择复制->复制链接地址
url的格式为:
https://raw.githubusercontent.com///?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
在终端:
wget-O myFilenamehttps://raw.githubusercontent.com///?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
链接仅在有限的时间内有效,或者您可以创建您的令牌:令人惊讶的是,在我找到解决方法之前,没有一个答案对我有效
您可以使用@thomasfuchs回答的个人访问令牌
注意:创建令牌时,必须检查管理员权限。见相关问题
因此,如果原始文件(登录时)的url为
注意:我们为GitHub Enterprise和API v3提供了API v4,我的bash解决方案如下所示(包括令牌清理/隐私):
TOKEN=yourTokenHere;历史记录-d$((HISTCMD-1))>/dev/null
curl-H“授权:令牌$token”\
-H'Accept:application/vnd.github.v3.raw'\
-o file.ext\
-Lhttp://github.company.com/api/v3/repos/[org]/[repo]/contents/path/file.ext?ref=[branch]
未兑现代币
我认为发行个人访问令牌有点危险,而且不是一个好办法,它可以访问所有存储库,甚至只是从我的私人存储库下载一个文件
如何-
我很想建议对单个文件使用带有令牌的url。别担心。github将自动生成令牌字符串。您可以在源代码页上获取此url
通过curl或wget等进入源代码页,下载您想下载的内容
找到“原始”按钮并单击它。
新页面已打开,只需复制url即可。此url如下所示:
()
您可以使用此url下载文件
更简单的解决方案是使用gh
首先,您必须登录:
对我来说,这个命令不是必需的,因为我已经登录了
然后,我们需要API URL定位要下载的文件。并调用gh
将其转换为经过身份验证的下载URL:
一个真实的例子也许更好。此处可从gh
cli下载:
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\u URL
中:
- 用户
owner
是cli
- 存储库名称
repo
也是cli
- 文件路径(
Path/file.ext
)是docs/install\u linux.md
我能够让它在github enterprise上运行,感谢上面的建议。我不得不接受你所有的建议并努力,最后我终于能够让它发挥作用。这些是我让它工作的步骤
- 创建个人令牌,请执行以下步骤:
- 确保您对令牌至少具有以下权限:
- 回购(选择回购下的全部)
- 管理:组织->阅读:组织(选择“阅读:或
$ curl -s https://1bacnotmyrealtoken123beefbea@raw.githubusercontent.com/concourse/concourse/master/README.md
....
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]
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
gh auth login
API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
curl $(gh api $API_URL --jq .download_url) -o file.ext
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
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
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
}]
}