title: How to create a gist on github with basic Authentication in C#
body: <p>I use C# .net 4.0 and I would like to create a public gist with my username and password (using basic authentication) on github. <a href="http://developer.github.com/v3/auth/#basic-authentication" rel="nofollow">http://developer.github.com/v3/auth/#basic-authentication</a></p>
  
  <p>If I use the correct password, I get the response: <code>The remote server returned an error: (404) Not Found.</code></p>
  
  <p>If I use a wrong password, I get the response: <code>The remote server returned an error: (401) Unauthorized.</code></p>
  
  <p>How can I authenticate and post a public gist under my username?</p>
  
  <p><a href="http://developer.github.com/v3/gists/#create-a-gist" rel="nofollow">http://developer.github.com/v3/gists/#create-a-gist</a>. the json I try to send:</p>
  
  <pre><code>{
      "description": "the description for this gist",
      "public": true,
      "files": {
          "file1.txt": {
              "content": "my new content "
          }
      }
  }
  </code></pre>
  
  <p>C# .NET 4.0 code</p>
  
  <pre><code>private void button_createGist_Click(object sender, EventArgs e)
      {
          String jsonMessage = "{ \"description\": \"the description for this gist\",  \"public\": true,"
                     + "\"files\": {   \"file1.txt\": {"
                     + "\"content\":\"my new content \"  } }}";
  
          String _url = "https://api.github.com/gists/";
  
          HttpWebRequest req = WebRequest.Create(new Uri(_url)) as HttpWebRequest;
  
          String userName = "myUserName";
          String userPassword = "pass123";  
          string authInfo = userName + ":" + userPassword;
          authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
  
          req.Method = "POST";
          req.ContentType = "application/json";
          // req.Headers.Add(string.Format("Authorization: token {0}", oauthToken));
           //req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
          req.Headers.Add("Authorization", "Basic " + authInfo);
          StreamWriter writer = new StreamWriter(req.GetRequestStream());
  
          System.Diagnostics.Debug.WriteLine(jsonMessage);
          writer.Write(jsonMessage);
          writer.Close();
  
          string result = null;
          using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)//Exception here
          {
              StreamReader reader =
                  new StreamReader(resp.GetResponseStream());
              result = reader.ReadToEnd();
              System.Diagnostics.Debug.WriteLine(result);
          }
      }
  </code></pre>