Mat*_*son 61 curl oauth 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
Run Code Online (Sandbox Code Playgroud)
tho*_*chs 77
以前的答案不起作用(或不再起作用).
您可以使用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参考如下:
the*_*ain 16
或者,您可以使用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>
Run Code Online (Sandbox Code Playgroud)
例:
$ curl -s https://1bacnotmyrealtoken123beefbea@raw.githubusercontent.com/concourse/concourse/master/README.md
....
Run Code Online (Sandbox Code Playgroud)
Jea*_*oto 15
一个更简单的解决方案是使用官方 GitHub CLI gh。
gh auth login
Run Code Online (Sandbox Code Playgroud)
对我来说,这个命令不是必需的,因为我已经登录了。
gh下载:API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
gh api $API_URL -H "Accept: application/vnd.github.raw" > file.ext
Run Code Online (Sandbox Code Playgroud)
一个真实的例子也许更好。这里是从cli下载install_linux.md :gh
API_URL=https://api.github.com/repos/cli/cli/contents/docs/install_linux.md
gh api $API_URL -H "Accept: application/vnd.github.raw" > install_linux.md
Run Code Online (Sandbox Code Playgroud)
在API_URL:
owner是clirepo太clipath/file.ext) 的路径是docs/install_linux.mdmar*_*mos 10
我知道这是一个老问题,但上面提出的解决方案都不适用于我.从那时起,API可能已经发生了变化.
这有效:
curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]
小智 6
我为此苦苦挣扎了几分钟,直到我意识到所需要做的就是将 url 用引号括起来以逃避&符号。
curl "https://raw.github.com/org/repo/file?login=username&token=the_token"
Run Code Online (Sandbox Code Playgroud)
这在我的私人回购中对我有用。
或者,如果您没有令牌:
curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
Run Code Online (Sandbox Code Playgroud)
我从应用程序安装中获得了令牌。
以前,您可以使用 查询?access_token=MY_TOKEN,但该查询已被弃用并最终于 2021 年 9 月删除。
在他们关于使用 GitHub 应用程序进行身份验证的文档中,他们说您可以x-access-token使用 URL 中的访问令牌和用户名来克隆存储库。
这似乎也适用于下载原始文件(这是令ghs_...牌):
$> curl "https://x-access-token:ghs_4qgGKx4skAcaF3bAb3scrTkN4@raw.githubusercontent.com/Octocat/codertocat/main/README.md"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
53921 次 |
| 最近记录: |