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

Viewed 17

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.

        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));
        }
        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);
        }
        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);
        }
        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?

0 Answers
Related