[英]How to create a gist on github with basic Authentication in C#
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. 我使用C#.net 4.0,我想在github上用我的用户名和密码(使用基本身份验证)创建一个公共要点。 http://developer.github.com/v3/auth/#basic-authentication http://developer.github.com/v3/auth/#basic-authentication
If I use the correct password, I get the response: The remote server returned an error: (404) Not Found.
如果我使用正确的密码,我得到响应: The remote server returned an error: (404) Not Found.
If I use a wrong password, I get the response: The remote server returned an error: (401) Unauthorized.
如果我使用了错误的密码,我会收到回复: The remote server returned an error: (401) Unauthorized.
How can I authenticate and post a public gist under my username? 如何在用户名下验证和发布公开要点?
http://developer.github.com/v3/gists/#create-a-gist . http://developer.github.com/v3/gists/#create-a-gist 。 the json I try to send: 我尝试发送的json:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "my new content "
}
}
}
C# .NET 4.0 code C#.NET 4.0代码
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);
}
}
I will keep the full example online. 我将在线保留完整的示例。 The posted example in the question should work if you change 如果您更改,问题中的已发布示例应该有效
String _url = "https://api.github.com/gists/";
to 至
String _url = "https://api.github.com/gists";
Use /gists
instead of /gists/
使用/gists
代替/gists/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.