使用C#更新任务

时间:2016-07-17 15:01:31

标签: c# .net asana-api

我正在尝试使用C#对我们的Asana项目进行API调用。我可以在没有问题的情况下“获取”特定项目下的所有任务。我也可以创建一个没有任何问题的“发布”新任务。我很难重新使用这个用于create / post的代码来执行现有Task的“put / update”。我曾尝试在传递JSON数据时尽可能多地指定信息,但未成功更新。我的主要目标是,只需能够通过API标记完成的任务。如果有人有什么建议/建议可以防止我成功更新/调用Asana任务,那将是非常棒的。谢谢大家的时间。

更新代码 - 无效

public static bool UpdateTask(string taskId)
    {
        string json = null;
        byte[] bytes = null;
        string url = "https://app.asana.com/api/1.0/tasks";
        HttpWebRequest req = default(HttpWebRequest);
        Stream reqStream = default(Stream);
        string authInfo = null;
        AsanaUpdateTask TaskData = new AsanaUpdateTask();
        try
        {
            authInfo = AsanaApiKey + Convert.ToString(":");

            TaskData.task = taskId;
            TaskData.name = "Business Collaboration";
            TaskData.assignee = "me";
            TaskData.assignee_status = "upcoming";
            TaskData.completed = "true";
            TaskData.due_on = "null";
            TaskData.notes = "new notes from API call 936am";

            json = JsonConvert.SerializeObject(TaskData);
            json = Convert.ToString("{ \"data\":") + json + "}";

            bytes = Encoding.UTF8.GetBytes(json);

            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = WebRequestMethods.Http.Put;
            req.ContentType = "application/json";
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
            req.ContentLength = bytes.Length;

            reqStream = req.GetRequestStream();
            reqStream.Write(bytes, 0, bytes.Length);
            reqStream.Close();


            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Console.WriteLine(res);
            Console.ReadLine();

            string finalString = res.Remove(0, 8);
            finalString = finalString.Remove((finalString.Length - 1));
            var newtask = JsonConvert.DeserializeObject(finalString);

            return true;
        }
        catch (WebException ex)
        {
            HttpWebResponse response = (HttpWebResponse)ex.Response;
            string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
        }
        return false;
    }

更新的JSON数据

{ "data":{"task":"156272920352856","name":"Business Collaboration","assignee":"me","assignee_status":"upcoming","completed":"true","due_on":"null","notes":"new notes from API call 936am"}}

创建/发布任务代码 - 目前正在运作

public static void CreateTask(string workspaceId, string projectId)
{
    string json = null;
    byte[] bytes = null;
    string url = "https://app.asana.com/api/1.0/tasks";
    HttpWebRequest req = default(HttpWebRequest);
    Stream reqStream = default(Stream);
    string authInfo = null;
    AsanaCreateTask TaskData = new AsanaCreateTask();
    try
    {
        authInfo = AsanaApiKey + Convert.ToString(":");
        TaskData.workspace = workspaceId;
        TaskData.name = "Business Collaboration";
        TaskData.notes = "new api notes 947am";

        json = JsonConvert.SerializeObject(TaskData);
        json = json.Insert((json.Length - 1), ",\"projects\":[" + projectId + "]");
        json = Convert.ToString("{ \"data\":") + json + "}";

        bytes = Encoding.UTF8.GetBytes(json);

        req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = WebRequestMethods.Http.Post;
        req.ContentType = "application/json";
        req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
        req.ContentLength = bytes.Length;

        reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();


        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(res);
        Console.ReadLine();

        string finalString = res.Remove(0, 8);
        finalString = finalString.Remove((finalString.Length - 1));
        var newtask = JsonConvert.DeserializeObject(finalString);
    }
    catch (WebException ex)
    {
        HttpWebResponse response = (HttpWebResponse)ex.Response;
        string resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }
}

创建的JSON数据

{ "data":{"notes":"new api notes 956am","workspace":"143129080097680","name":"Business Collaboration","projects":[143129080097684]}}

1 个答案:

答案 0 :(得分:1)

我在gitHub上看了一下AsanaNet,这提供了更多的见解以及关于URL中的直接taskId的Marks评论。这是Asana的一个完全工作的“更新任务”方法。想想有一天它会帮助其他人。

我创建了一个可以序列化/反序列化为JSON的小对象。

public class AsanaUpdateTask
{
    public string notes { get; set; }
    public string workspace { get; set; }
    public string name { get; set; }
    public bool completed { get; set; }
}

以下是成功“放置”更新的方法。

public static void UpdateTask(string workspaceId, string taskId)
{
    string json = null;
    byte[] bytes = null;
    string url = string.Format("https://app.asana.com/api/1.0/tasks/{0}", taskId);
    HttpWebRequest req = default(HttpWebRequest);
    Stream reqStream = default(Stream);
    string authInfo = null;
    AsanaUpdateTask TaskData = new AsanaUpdateTask();
    try
    {
        authInfo = BearerToken + Convert.ToString(":");
        /*Required Minimum for a response from API::: workspace, name, and notes*/
        TaskData.workspace = workspaceId; 
        TaskData.name = "Business Collaboration V7";
        TaskData.notes = "1306:: this is working..updated from api Aug 26";
        TaskData.completed = false; //Used to mark task completed

        json = JsonConvert.SerializeObject(TaskData);
        json = Convert.ToString("{ \"data\":") + json + "}";

        bytes = Encoding.UTF8.GetBytes(json);

        req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = WebRequestMethods.Http.Put;
        req.ContentType = "application/json";
        req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(authInfo)));
        req.ContentLength = bytes.Length;

        reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();


        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
        string res = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(res);
        Console.ReadLine();

        string finalString = res.Remove(0, 8);
        finalString = finalString.Remove((finalString.Length - 1));
        var newtask = JsonConvert.DeserializeObject(finalString);
    }
    catch (WebException ex)
    {
        HttpWebResponse response = (HttpWebResponse)ex.Response;
        string readableString = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    }
}