Command line 如何使用命令行从privaterepo下载GitHub发行版

Command line 如何使用命令行从privaterepo下载GitHub发行版,command-line,github,Command Line,Github,解释了两种授权方式,但看起来这两种方式都不适用于发布文件 由于: curl-u'username'-L-o a.tgzhttps://github.com/company/repository/releases/download/TAG-NAME/A.tgz 总有这样的事情 似乎这两种身份验证方法只适用于API端点。 有一个用于下载发布资产()的API端点: 但你需要知道资产的数字ID。 我询问他们的支持人员,他们是否可以添加一个API端点来按名称下载发布资产(就像Tarball一样) 更新

解释了两种授权方式,但看起来这两种方式都不适用于发布文件

由于:

curl-u'username'-L-o a.tgzhttps://github.com/company/repository/releases/download/TAG-NAME/A.tgz

总有这样的事情



似乎这两种身份验证方法只适用于API端点。 有一个用于下载发布资产()的API端点:

但你需要知道资产的数字ID。 我询问他们的支持人员,他们是否可以添加一个API端点来按名称下载发布资产(就像Tarball一样)


更新:Github支持部门的响应:

我们没有像你建议的那样提供任何API。我们试图避免脆弱的URL,如果发布资产的标签或文件名发生变化,这些URL就会发生变化。我将把您的反馈意见带到团队中进一步讨论


我在这篇评论中找到了答案:

curl
正在将请求中的身份验证头转发到AmazonS3存储桶,Github发布资产存储在该存储桶中。 来自S3的错误响应:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
 <Code>InvalidArgument</Code>
 <Message>
   Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified
 </Message>
 <ArgumentName>Authorization</ArgumentName>
 <ArgumentValue>token <yourtoken> </ArgumentValue><RequestId>4BEDDBA630688865</RequestId> <HostId>SXLsRKgKM6tPa/K7g7tSOWmQEqowG/4kf6cwOmnpObXrSzUt4bzOFuihmzxK6+gx</HostId>
</Error>
单线wget解决方案:

wget --auth-no-challenge --header='Accept:application/octet-stream' https://<token>:@api.github.com/repos/:owner/:repo/releases/assets/:id -O app.zip
wget--auth no challenge--header='Accept:application/octet stream'https://:@api.github.com/repos/:owner/:repo/releases/assets/:id-O app.zip

请尝试:
curl-i-H“Authorization:token”-H“Accept:application/octet stream”https://:@api.github.com/repos/:owner/:repo/releases/assets/:id
,了解更多详细信息。添加
-L
以查看S3错误消息。

更简单的解决方案是使用.netrc存储凭据。这样,curl就不会将凭证转发到amazons3 Bucket

~/.netrc
文件中(应使用0600文件权限创建):

然后使用curl-n选项使用.netrc:

curl -L -O -J -n -H "Accept:application/octet-stream" https://api.github.com/repos/:owner/:repo/releases/assets/:id
下面是一个“一行程序”,使用
wget
进行HTTP请求,使用
python
进行JSON解析:

(export AUTH_TOKEN=<oauth-token>; \
 export ASSET_ID=$(wget -O - https://api.github.com/repos/<owner>/<repo>/releases/tags/<tag>?access_token=$AUTH_TOKEN | python -c 'import sys, json; print json.load(sys.stdin)["assets"][0]["id"]'); \
 wget --header='Accept:application/octet-stream' -O <download-name> https://api.github.com/repos/<owner>/<repo>/releases/assets/$ASSET_ID?access_token=$AUTH_TOKEN)
(导出身份验证令牌=\
导出资产ID=$(wget-O-https://api.github.com/repos///releases/tags/?access_token=$AUTH_TOKEN | python-c'导入sys,json;打印json.load(sys.stdin)[“资产”][0][“id”]”\
wget--header='Accept:application/octet-stream'-Ohttps://api.github.com/repos///releases/assets/$ASSET\u ID?访问令牌=$AUTH\u令牌)
要使用它,只需用适当的值替换

说明:

  • 第一条语句(
    export AUTH_TOKEN=
    )设置后续的
    wget
    命令所使用的
  • 第二项声明分为两部分:
  • wget-O-https://api.github.com/repos///releases/tags/?access_token=$AUTH_TOKEN
    part并将其打印在标准输出上
  • python-c'导入sys,json;print json.load(sys.stdin)[“assets”][0][“id”]”
    part从stdin解析json并提取(第一个)发布资产的
    id
  • 第三条语句(
    wget--header='Accept:application/octetstream'-O.tar.gz)https://api.github.com/repos///releases/assets/$ASSET\u ID?访问\u令牌=$AUTH\u令牌)
    )并将其存储到文件中
  • 外圆括号创建一个子shell,并确保导出的环境变量随后被丢弃

要从私有回购下载发布文件,您可以使用个人访问令牌,该令牌可以在完全控制私有存储库范围的情况下生成

然后使用
curl
命令下载资产(使用适当的值进行更改):

或者,如果您正在使用OAuth应用程序,请使用:

curl -u my_client_id:my_client_secret https://api.github.com/repos/:owner/:repo/releases/assets/:id
其中:

  • :所有者
    是您的用户或组织用户名
  • :repo
    是您的存储库名称
  • :id
    是您的资产id,可在标记发布URL中找到,如:

    https://api.github.com/repos/:owner/:repo/releases/tags/:tag 
    
  • :令牌
    是您的个人访问令牌(可在
    /settings/tokens
    中创建)

注意:使用
access\u令牌
作为查询参数是不正确的

见:


下面是Bash脚本,它可以下载给定文件特定名称的资产文件:

#!/usr/bin/env bash
# Script to download asset file from tag release using GitHub API v3.
# See: http://stackoverflow.com/a/35688093/55075    
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"

# Check dependencies.
set -e
type curl grep sed tr >&2
xargs=$(which gxargs || which xargs)

# Validate settings.
[ -f ~/.secrets ] && source ~/.secrets
[ "$GITHUB_API_TOKEN" ] || { echo "Error: Please define GITHUB_API_TOKEN variable." >&2; exit 1; }
[ $# -ne 4 ] && { echo "Usage: $0 [owner] [repo] [tag] [name]"; exit 1; }
[ "$TRACE" ] && set -x
read owner repo tag name <<<$@

# Define variables.
GH_API="https://api.github.com"
GH_REPO="$GH_API/repos/$owner/$repo"
GH_TAGS="$GH_REPO/releases/tags/$tag"
AUTH="Authorization: token $GITHUB_API_TOKEN"
WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
CURL_ARGS="-LJO#"

# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!";  exit 1; }

# Read asset tags.
response=$(curl -sH "$AUTH" $GH_TAGS)
# Get ID of the asset based on given name.
eval $(echo "$response" | grep -C3 "name.:.\+$name" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
#id=$(echo "$response" | jq --arg name "$name" '.assets[] | select(.name == $name).id') # If jq is installed, this can be used instead. 
[ "$id" ] || { echo "Error: Failed to get asset id, response: $response" | awk 'length($0)<100' >&2; exit 1; }
GH_ASSET="$GH_REPO/releases/assets/$id"

# Download asset file.
echo "Downloading asset..." >&2
curl $CURL_ARGS -H "Authorization: token $GITHUB_API_TOKEN" -H 'Accept: application/octet-stream' "$GH_ASSET"
echo "$0 done." >&2
脚本使用示例:

./get_gh_asset.sh :owner :repo :tag :name
其中name是您的文件名(或部分文件名)。在脚本前面加上
TRACE=1
以调试它


如果您想知道为什么
curl
有时会失败(如其他答案中所述):

只允许一种身份验证机制;只应指定
X-Amz-Algorithm
查询参数、签名查询字符串参数或
授权

当你像这样跑步时:

curl -vLJ -H 'Authorization: token <token>' -H 'Accept: application/octet-stream' https://api.github.com/repos/:owner/:repo/releases/assets/<id>
curl-vLJ-H'授权:令牌'-H'接受:应用程序/八位字节流'https://api.github.com/repos/:owner/:repo/releases/assets/
这是因为您同时指定了多个机制,因此S3服务器不知道要使用哪个机制,因此只能选择一个,例如:

  • X-Amz-Algorithm
    查询参数
  • 签名查询字符串参数(
    X-Amz-Signature
  • 授权头(
    授权:令牌

由于GitHub会从资产页面重定向您(当请求
应用程序/octet流
时),它会在查询字符串中自动填充凭据,并且由于
curl
会在请求头中传递相同的凭据(您已指定),因此它们相互冲突。因此,作为解决方法,您可以使用
access\u token

我们必须经常从私有GitHub repos下载发布资产,因此我们创建了一个开源的跨平台工具,它可以轻松地从git标记、提交或公共分支下载源文件和发布资产以及私人GitHub回购协议

例如,要从私有GitHub回购的版本
0.1.3
下载发行资产
foo.exe
,请执行以下操作:

GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --release-asset="foo.exe" /tmp
这里
GITHUB_API_TOKEN=XXX
./get_gh_asset.sh :owner :repo :tag :name
curl -vLJ -H 'Authorization: token <token>' -H 'Accept: application/octet-stream' https://api.github.com/repos/:owner/:repo/releases/assets/<id>
GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --release-asset="foo.exe" /tmp
CURL="curl -H 'Authorization: token <auth_token>' \
      https://api.github.com/repos/<owner>/<repo>/releases"; \
ASSET_ID=$(eval "$CURL/tags/<tag>" | jq .assets[0].id); \
eval "$CURL/assets/$ASSET_ID -LJOH 'Accept: application/octet-stream'"
CURL="curl -u <github_user> https://api.github.com/repos/<owner>/<repo>/releases"; \
ASSET_ID=$(eval "$CURL/tags/<tag>" | jq .assets[0].id); \
eval "$CURL/assets/$ASSET_ID -LJOH 'Accept: application/octet-stream'"