Having difficulty creating a Task via API


#1

Background: JavaScript (V8) code being controlled by C# via ClearScript. It’s a project called Lychen.

This is the code creating the JSON packet:

    // ct - create task - ct/project/assignee/name/notes

var Console = CS.System.Console;
var DateTime = CS.System.DateTime;
var File = CS.System.IO.File;
var Path = CS.System.IO.Path;

if (ARGUMENTS.length < 5) {
    Console.WriteLine("{0} needs four parameters (/project/assignee/name/notes)", ARGUMENTS[0]);
} else {
    REQUEST = new RESTSHARP.RestRequest("api/1.0/tasks", RESTSHARP.Method.POST);
    var token = ASANA_SETTINGS.IniReadValue("Authorisation", "Token", "");
    if (token === "") {
        Console.WriteLine("Token not found in AsanaSettings.INI");
    } else {
        REQUEST.AddHeader("Authorization", "Bearer " + token);
        REQUEST.AddHeader("Content-Type", "application/json");
        var json = JSON.stringify({
            projects: [{ gid: ARGUMENTS[1] }],
            assignee: { gid: ARGUMENTS[2] },
            name: ARGUMENTS[3],
            notes: ARGUMENTS[4],
            workspace: { gid: WORKSPACE }
        });
        REQUEST.AddJsonBody(json);
        RESPONSE = CLIENT.Execute(REQUEST);

        var tag = DateTime.Now.ToString("yyyy'-'MM'-'dd'-'HH'-'mm");
        File.WriteAllText(Path.Combine("log", "ct_" + tag + ".json"), RESPONSE.Content);

    }
}

This is the packet of JSON:

{"projects":[{"gid":"1113266773900829"}],"assignee":{"gid":"9814050857874"},"name":"Task 1","notes":"Added we assume to Test Project","workspace":{"gid":"9814050864025"}}

And this is the error message:

{"errors":[{"message":"You should specify one of workspace, parent, projects","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

What I don’t get is that there is a projects and a workspace. And it’s not a subtask so I don’t need a parent (and I have tried putting a null parent in to no effect.)

Where’s the problem?


#2

To partially answer my own question, I need to put {data:{}} around the request.

Now I get a different error message:

{"errors":[{"message":"workspace: Not a valid GID type: \"object\".","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

#3

GID are string, maybe you are passing a number?


#4

I did think of that, but as you can see from the original posting:

"workspace":{"gid":"9814050864025"}

#5

Where you’re specifying related objects like projects. assignee, workspace - don’t try to put it in as an object, just put the gid value itself. In other words, build your json to look like this (I edited this json by hand and didn’t test it explicitly - it should be correct but anyway you’ll get the idea):

{"projects":["1113266773900829"],"assignee":"9814050857874","name":"Task 1","notes":"Added we assume to Test Project","workspace":"9814050864025"}