2014-05-14 16 views
0

當我嘗試運行下面的代碼時,出現錯誤代碼:名稱太短(最少爲1個字符)。我正在使用RestSharp v104.1。如果我嘗試閱讀,我沒有任何問題,就在我嘗試創建新用戶時。使用RestSharp的ZenDesk錯誤:名稱太短

static string requestUri = "https://subdomain.zendesk.com/api/v2"; 

    static string username = "myusername"; 
    static string password = "mypassword"; 

    static void Main(string[] args) 
    { 
     string new_users = "{\"user\":{ \"id\":4000000001, \"name\":\"Robin 001\", \"email\":\"[email protected]\" }}"; 

     Console.WriteLine(WriteJSON(username, password, "/users.json", new_users)); 
     Console.ReadLine(); 
    } 

    public static string WriteJSON(string username, string password, string json, string to_write) 
    { 
     var client = new RestClient(requestUri); 
     client.Authenticator = new HttpBasicAuthenticator(username, password); 
     client.AddDefaultHeader("Content-type", "application/json"); 
     client.UserAgent = "MozillaXYZ/1.0"; 
     client.FollowRedirects = true; 
     client.MaxRedirects = 10; 

     var request = new RestRequest(json); 
     request.Method = Method.POST; 
     request.RequestFormat = DataFormat.Json; 
     request.AddBody(to_write); 

     IRestResponse response = client.Execute(request); 
     var content = response.Content; 

     System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(content, "root"); 
     return node.ToString(); 
    } 

回答