2013-03-14 13 views
8

vedo che il percorso di download per un repo GitHub ha la formaC# esempio di scaricare GitHub repo privato a livello di codice

https://github.com/{username}/{reponame}/archive/{branchname}.zip 

Per un repo privato, comprensibilmente è necessario fornire le credenziali per scaricare il repo, qualcuno può fornire un esempio C# su come fornire un'autenticazione di base HTTPS in modo da poter scaricare il repository in modo programmatico?

Grazie,

risposta

0

con CURL:

curl -L -F "login=$USER" -F "token=$TOKEN" https://github.com/$USER/$REPO/$PKGTYPE/$BRANCHorTAG 

dove $ token è il token API sul tuo profilo GitHub, non un token OAuth2 utilizzato per comunicare con l'APIv3.

$ USER è l'account utente a cui è collegato il token, non necessariamente l'organizzazione/altro utente a cui appartiene il repository. La seconda istanza di $ USER è l'utente/account del repository.

$ REPO è il nome del repository privato

$ PKGTYPE è tarball o zipball e $ BRANCHorTAG è un ramo, come il padrone, o un nome di tag per un commit.

La prima istanza di $ USER deve avere accesso al repository appartenente alla seconda istanza di $ USER.

Non ho potuto trovare questo documento DOVUNQUE, quindi ho anche un po 'di scrivere su di esso se volete qualcosa di più dettagliato.

+1

So che si può usare 'curl' da altre questioni, ma non posso supporre che 'curl' sia installato sulla macchina, quindi dobbiamo usare le primitive .Net di base, sfortunatamente. – theburningmonk

-1

Sto esaminando il Okctokit.Net attualmente. Dagli Un colpo. NuGet: Installare-Pacchetto Octokit

4

Ecco un puro C# modo:

var githubToken = "[token]"; 
var url = "https://github.com/[username]/[repository]/archive/[sha1|tag].zip"; 
var path = @"[local path]"; 

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); 
    var contents = client.GetByteArrayAsync(url).Result; 
    System.IO.File.WriteAllBytes(path, contents); 
} 
+0

Mi rendo conto che questa è una risposta molto vecchia, ma ho difficoltà a farlo funzionare; restituisce sempre 404 anche se scarica normalmente quando inserisco l'url in un browser. C'è qualcosa che mi manca? –

0

tramite un token github (https://help.github.com/articles/creating-an-access-token-for-command-line-use)

 var githubToken = "token"; 
     var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/$OWNER/$REPO/contents/$PATH"); 
     request.Headers.Add(HttpRequestHeader.Authorization, string.Concat("token ", githubToken)); 
     request.Accept = "application/vnd.github.v3.raw"; 
     request.UserAgent = "test app"; //user agent is required https://developer.github.com/v3/#user-agent-required 
     using (var response = request.GetResponse()) 
     { 
      var encoding = System.Text.ASCIIEncoding.UTF8; 
      using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding)) 
      { 
       var fileContent = reader.ReadToEnd(); 
      } 
     } 
Problemi correlati