简体   繁体   中英

C#: Download Release Asset from Github

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.

Based on the documentation ( https://docs.github.com/en/enterprise-server@2.22/rest/overview/other-authentication-methods#via-oauth-and-personal-access-tokens ) my best guess is, that your crendentials are wrong.

The docs say, the format should be username:token , yet you are only using token followed by a colon : - that doesn't look right to me, either.

So essentially you need to refactor your credentials variable a bit:

var credentials = $"{username}:{githubToken}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM