简体   繁体   中英

Downloading Latest Release from a GitHub Repository using Octokit.net

How can I download the latest release of a private repository as a zip-file programmatically using Octokit.net?

I am not looking to download the entire repository, just the source code from a specified release, for example the latest one.

So far, I have created a GitHubClient, authorized with a GitHubToken, and I have gotten a list of all releases in the repo. I'm able to get information on the latest release, like the name, tag, Url, and ZipballUrl, etc.

I've attempted to download the release using the ZipballUrl, but it does not return a valid zip.

In my code below, I'm trying to download the latest release from the Octokit repo.

    String workspaceName = "octokit";
    String repositoryName = "octokit.net";
    String filename = "temp.zip";

    var client = new GitHubClient(new ProductHeaderValue(repositoryName));
    String GitHubToken = "--- token goes here ---";
    var tokenAuth = new Credentials(GitHubToken);
    client.Credentials = tokenAuth;
    
    // Retrieve a List of Releases in the Repository, and get latest using [0]-subscript
    var releases = await client.Repository.Release.GetAll(workspaceName, repositoryName);
    var latest = releases[0];

    // Get a HttpResponse from the Zipball URL, which is then converted to a byte array
    var response = await client.Connection.Get<object>(new Uri(latest.ZipballUrl), new Dictionary<string, string>(), "application/json");
    byte[] releaseBytes = Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString());

    // Create the resulting file using the byte array
    await File.WriteAllBytesAsync(filename, releaseBytes);

I'm not very familiar with the GitHubClient, nor Http- or WebClients, so I cannot tell if the mistake lies in the HttpResponse and the content I get from there, or anywhere else in the file writing process.

SOLVED:

I could not solve this entirely using the GitHubClient, so I am still interested to know if that is possible.

However, I managed to solve this by adding a WebClient to take care of the download after finding the ZipballUrl using Octokit. This had to be configured with User-Agent and Authorization headers to work.

var latest = releases[0];
           
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Anything");
webClient.Headers.Add("authorization", "token " + GitHubToken);
await webClient.DownloadFileTaskAsync(new Uri(latest.ZipballUrl), filename);

To complement @haakonfp's own answer to this question I'd like to point out, for the GitHub API to not return a 403, a User-Agent header is needed in the request headers. You probably need a token only if you're trying to access a private repo, but in either case, said header will allow you to download the release contents.

var wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.UserAgent, "MyUserAgent");
wc.DownloadFile(latestVersion.ZipballUrl, "release.zip");

Source: https://stackoverflow.com/a/37142247/10177659

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