Zendesk自定义用户/组织字段

时间:2013-11-18 19:32:52

标签: .net zendesk

好的,所以我在zendesk遇到了两个可能的解决方案的问题,但不知道其中任何一个是否会起作用。我们的系统内置了为每个客户和组织创建的自定义字段。首先我尝试使用C#包装器,但是如果有我无法找到它,则无法通过这些方法访问自定义字段。

因此,如果没有办法通过C#包装器执行此操作,我试图通过完全休息来完成此操作。这是代码。

string url = "https://{mydomain}.zendesk.com/api/v2/user_fields/7.json";
        string creds = Convert.ToString(Encoding.UTF8.GetBytes(EMAIL + "/"+ TOKEN));
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Headers.Add("Authorization", "Basic" + creds);

        request.Method = "GET";

        HttpWebResponse resp = request.GetResponse() as HttpWebResponse;

        if (resp.StatusCode == HttpStatusCode.OK)
        {
            using (Stream respStream = resp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
                string test = reader.ReadToEnd();
            }
        }
        else
        {
            Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
        }

现在所有这一切应该是得到一个响应,将返回所有列的名称给我,但是我一直得到401未经授权的响应。关于我做错了什么的建议?

1 个答案:

答案 0 :(得分:0)

对于Zendesk c#wrapper ...

IndividualTicketResponse tr = zapi.Tickets.GetTicket(9);
        var customField = tr.Ticket.CustomFields.Where(x => x.Id == 23653058).Single();//.Select(x => x.Id).Single();
        customField.Value = "Updated via c# wrapper";
        IndividualTicketResponse ticketResponse = zapi.Tickets.UpdateTicket(tr.Ticket);

我不使用令牌访问,但如果不使用包装器,这对我有用......

 string serverURL = "https://myzensubdomain.zendesk.com/api/v2/tickets/9.json";
        string userName = "username@domain.com";
        string userPassword = "mySecretPassword";


        TicketCustomFieldUpdate dataTicket = new TicketCustomFieldUpdate();


        CustomField cf = new CustomField();
        cf.id = 23653058;
        cf.value = "It worked!";
        dataTicket.ticket = new Ticket();
        dataTicket.ticket.custom_fields = new List<CustomField>();
        dataTicket.ticket.custom_fields.Add(cf);

        string stJSON = clsJSONHelper.JsonSerializer<TicketCustomFieldUpdate>(dataTicket);


        NetworkCredential myCred = new NetworkCredential(userName, userPassword, serverURL);
        CredentialCache myCache = new CredentialCache();
        myCache.Add(new Uri(serverURL), "Basic", myCred);

        HttpWebRequest wreq = (HttpWebRequest)HttpWebRequest.Create(serverURL);
        wreq.Credentials = new NetworkCredential(userName, userPassword);
        wreq.ContentLength = 0;            
        wreq.Method = "PUT";
        wreq.ContentType = "application/json";
        wreq.PreAuthenticate = true;


        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] byte1 = encoding.GetBytes(stJSON);
        wreq.ContentLength = byte1.Length;

        using (Stream strm = wreq.GetRequestStream())
        {
            strm.Write(byte1, 0, byte1.Length);
        }

        var wresp = (HttpWebResponse)wreq.GetResponse();
        using (var strmr = new StreamReader(wresp.GetResponseStream()))
        {
            var responseText = strmr.ReadToEnd();
            context.Response.Write(responseText);
        }