如何使用Powershell获取Github Release Asset的二进制版本?

时间:2015-09-25 21:13:10

标签: powershell-v3.0 github-api

我正在关注GitHub Releases的文档,但我得到的只是资产内容。

这是我的powershell脚本:

$gitHubRepository = "<repo>"
$gitHubUsername = "<username>"
$gitHubApiKey = "<apikey>"

# Locate Latest GitHub Release
$releaseParams = @{
    Uri = "https://api.github.com/repos/$gitHubUsername/$gitHubRepository/releases";
    Method = 'GET';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String(
            [Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic")
        );
    }
    ContentType = 'application/json';
    Body = (ConvertTo-Json $releaseData -Compress)
}

$result = Invoke-RestMethod @releaseParams

$tag = $result.tag_name[0]
Write-Host "Release $tag Found."

$asset = $result.assets[0].url
$ZipFile = $result.assets[0].name
$asset

$releaseAssetParams = @{
    Uri = $asset;
    Method = 'GET';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($gitHubApiKey + ":x-oauth-basic"));
    }
    ContentType = 'application/octet-stream';
}

Invoke-RestMethod @releaseAssetParams -OutFile $ZipFile

执行此脚本(正确设置变量),输出资产内容,而不是预期的二进制(zip)文件。

内容类型是否未被识别?

1 个答案:

答案 0 :(得分:2)

您需要在application/octet-stream请求标头中指定Accept,而不是Content-Type请求标头,因为您要告诉服务器您要接受的内容,而不是您要接受的内容发送。

所以,你需要发送这样的标题:

Accept: application/octet-stream

而不是

Content-Type: application/octet-stream

来自API的响应将重定向到具有实际资产的URL,因此您需要发出另一个请求(这是预期的行为)。如果遇到问题,let us know