3

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.

Metal24
  • 31
  • 2

1 Answers1

1

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}";
Marco
  • 22,856
  • 9
  • 75
  • 124
  • You may also try a blank username with the PAT as the password. I've had success with github APIs using that format. But never the PAT as the username – pinkfloydx33 Apr 25 '21 at 23:09
  • Thanks for your suggestions. Unfortunatly it didnt work on my end. It seems to be the case that there is a general issue. No luck also with curl & gitlab cl, curls gives a OK 200 response, but still no data transmitted... so the topic is now handled by technical support. – Metal24 Apr 28 '21 at 00:09
  • @Metal24 When you find a solution, make sure to post it here. It may help others in the future. Good luck! – Marco Apr 28 '21 at 05:28