C#: Download Release Asset from Github

Viewed 249

I want to download release asset zipball´s in a C# application for further use. I´m using Octokit to get all release informations from the repo, including the respective browserdownload_url.

After some research it seemed to me, that you cant download this release asset zip´s via octokit, so trying with httpclient as suggested by some SO posts, that were asking these questions.

The release zip´s are on a Github Enterprise Repository, so they require Authentication. And that is probably my issue, i cant make the authentication work with the httpClient...

The request always responds with Code 404 (which is the regular behaviour if you try by putting the url into the regular browser without logging in)

My actual implementation looks like this

public void DownloadRelease(string dlUrl, string targetPath)
{
    var githubToken = "aaaaaaaaaaabbbbbbbbbcccccccccdddddddddd";   //Token created in the github developer settings with all available rights

    //dlUrl = https://github.server.de/organization/project/releases/download/v1.2.34/release.zip

    using (var client = new System.Net.Http.HttpClient())
    {
        var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
        credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
        //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", githubToken);
        var contents = client.GetByteArrayAsync(dlUrl).Result;
        System.IO.File.WriteAllBytes(targetPath, contents);
    }
}

Update: At the End we followed the way of using the curl way: https://docs.github.com/en/enterprise-server@3.0/rest/reference/repos#download-a-repository-archive-zip

And one more mistake on my end: There were releases without downloadable Asset IDs which i didnt catch in the code.

1 Answers
Related