2017-12-21 6 views
-1

githubに問題が発生しても動作しません。私は何らかの理由でそれから何かを取り戻すことができます。C#Github API問題を作成できません

私がしようとしているのは、POSTと同じ機能を持つjson形式の文字列をアップロードすることです。明らかにそれは動作していません。

using System; 
using System.Collections.Generic; 
using System.Net; 
using Newtonsoft.Json; 

namespace TestProject 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      CreateGithubIssue("Test Issue - C#", "This is just a test to check if i can manage to create an issue from C#."); 
     } 
     static string username = "someUser"; 
     static string password = "somePassword"; 

     static string repoIssueLink = "http://api.github.com/repos/someUser/someRepo/issues"; 

     public static void CreateGithubIssue(string Title, string Description) 
     { 

      WebClient webClient = new WebClient(); 
      webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
      webClient.Credentials = new NetworkCredential(username, password); 

      string jsonPost = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented); 

      string response = webClient.UploadString(repoIssueLink, jsonPost); 
      Console.WriteLine(response); 
      Console.WriteLine(jsonPost); 

     } 
    } 

    public class Issue 
    { 
     public string title; 
     public string body; 
     public List<string> assignees; 
     public int milestone; 
     public List<string> labels; 

     public Issue(string title = "Default Title", string body = "Default Body", List<string> assignees = null, int milestone = 0, List<string> labels = null) 
     { 
      if (assignees == null) assignees = new List<string>(); 
      if (labels == null) labels = new List<string>(); 

      this.title = title; 
      this.body = body; 
      this.assignees = assignees; 
      this.milestone = milestone; 
      this.labels = labels; 
     } 
    } 
} 

出力: Output

+0

あなたは二回、それをシリアル化している –

+0

ああ、おっと。私はそれを忘れてしまった。 –

+0

出力はどのようになっていますか? –

答えて

1

よしは、いくつかの他の人々を求めた後、私は答えを見つけることができました。 私は、HTTPSへのリンクを変更し、このコードを使用:

WebClient webClient = new WebClient(); 
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 

string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)); 

webClient.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials); 

string jsonOutput = JsonConvert.SerializeObject(new Issue(Title, Description), Formatting.Indented); 

string response = webClient.UploadString(repoIssueLink, jsonOutput); 
Console.WriteLine(response); 
Console.WriteLine(jsonOutput); 
関連する問題