BillHaggerty
BillHaggerty

Reputation: 6589

Given a path to a file assosiated with a github release how can I download it in c#

So when I make a release it triggers actions that build different projects in the solution. I want to be able to download a file that is added to the release via these actions. When I am logged in I can download the file using the following url: https://github.com/PhoeniqsTech/ForianSolution/releases/download/v0.0.1/Forian.BiotrackAgent-v0.0.0-test-win-x64.zip which I'm sure will result in a 404 for you since its a private repo.

I have tried to download this file using by personal access token with all rights given to it in the following ways but all I get is a 404 response.

Test 1

public async Task TestOne()
{
    using HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("AppName", "1.0"));
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", AccessToken);
    var response = await client.GetAsync(GithubZipfile);
    byte[] contents = await response.Content.ReadAsByteArrayAsync();
    Console.WriteLine(Encoding.ASCII.GetString(contents));
}

Test 2

public async Task TestTwo()
{
    using var client = new HttpClient();
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", AccessToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
    var contents = await client.GetByteArrayAsync(GithubZipfile);
    await File.WriteAllBytesAsync(LocalFile, contents);
}

Test 3

public async Task TestThree()
{
    using HttpClient client = new();
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "UserName:{0}", AccessToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
    var contents = await client.GetByteArrayAsync(GithubZipfile);
    await File.WriteAllBytesAsync(LocalFile, contents);
}

Test 4

public async Task TestFour()
{
    using HttpClient client = new();
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "UserName:{0}", AccessToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", credentials);
    var contents = await client.GetByteArrayAsync(GithubZipfile);
    await File.WriteAllBytesAsync(LocalFile, contents);
}

Has anyone else figured out how to download release files from GitHub programmably in dotnet?

Upvotes: 1

Views: 376

Answers (1)

BillHaggerty
BillHaggerty

Reputation: 6589

You cannot at this time download a file using that path outside of a browser. The "correct" way to download an asset is to first list the assets in a release:

Url: https://api.github.com/repos/PhoeniqsTech/ForianSolution/releases/tags/v0.0.3

Headers:

Authorization: Bearer {PersonalAccessToken}

Accept: application/vnd.github+json

This returns a result with all assets in a release. For each asset there is a url field that can be used to download the file. My example download request is configured as follows:

Url: https://api.github.com/repos/PhoeniqsTech/ForianSolution/releases/assets/78691863

Headers:

Authorization: Bearer {PersonalAccessToken}

Accept: application/octet-stream

Upvotes: 2

Related Questions