title: Access a file from Private GitHub Repository from C#
body: <p>Below is an example on how I've been accessing individual files from my public GitHub repository from a console application. Can I do the same for a private repository? I'm not sure how to change the code or if I need to access it with different authorisation settings. </p>
  
  <p>Can anyone tell me what I am doing wrong?</p>
  
  <p>I'm not sure is this will help <a href="https://developer.github.com/v3/guides/managing-deploy-keys/" rel="nofollow noreferrer">https://developer.github.com/v3/guides/managing-deploy-keys/</a> </p>
  
  <pre><code>    private static void Main(string[] args)
      {
          var file = $"https://raw.githubusercontent.com/{username}/{repositoryName}/{branch}/{filePath}";
  
          System.IO.File.WriteAllText($"c:\\{filePath}", GetFile(file));
  
          System.Console.ReadKey();
      }
  
      public static string GetFile(string input)
      {
          var output = string.Empty;
          HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(input);
  
          request.Headers.Add("Authorization", githubtoken);
          request.AllowAutoRedirect = true;
  
          var response = (HttpWebResponse)request.GetResponse();
  
          using (var stream = new System.IO.MemoryStream())
          {
              response.GetResponseStream().CopyTo(stream);
              output = System.Text.Encoding.ASCII.GetString(stream.ToArray());
          }
  
          response.Close();
  
          return output;
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>So based on feedback (thanks Olivier), it seems that any use of the githubtoken I used returns either a 403 or 404. My token had read:packages &amp; read:repo_hook and I thought that would be sufficient. I used the following <a href="https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line" rel="nofollow noreferrer">link</a> to show me how to create the token. Do I need anything else to access a private repo with a token? </p>
  
  <p>Can I get access to this via username and password and if so how do I change the above to get a file?</p>