使用 POST http 请求创建 Asana 任务

Creating an Asana Task using a POST http request

我正在尝试使用 asana-api 通过 POST http 请求创建任务,但我一直收到 400 错误请求作为响应.

我设法使用(GET 请求)从 Asana-api 获取数据,但我无法使用(POST 请求)[=14= 向 Asana 发送数据]

我正在使用 'request' 模块执行 api 调用

这是错误消息:

`{"errors":[{
      "message":"Could not parse request data,invalid JSON",
      "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"}
 ]}`

这是我的代码:

testTask(){
   var taskName = "Test Name for a Test Task"
   var workspaceID = "123456789"
   var projectID = "123456789"
   var assigneeID = "123456789"
   var parentID = null
   this.createTask(taskName, workspaceID, projectID, assigneeID, parentID)
}

createTask(taskName, workspaceID, projectID, assigneeID, parentID){
    var token = "0/1234abcd5678efgh9102ijk"
    var bearerToken = "Bearer " + token
    var task = {
       data: {
         assignee: "me",
         notes: "test test test test",
         workspace: workspaceID,
         name: taskName,
         projects: [projectID],
         parent: parentID
       }
     }
     var options = {
       "method" : "POST",
       "headers" : {"Authorization": bearerToken},
       "contentType": "application/json",
       "payload" : JSON.stringify(task)
     }
     try {
       var url = "https://app.asana.com/api/1.0/tasks";
       request.post(url, options, function optionalCallback(err, httpResponse, body) {
       if (err) {
          return console.error('upload failed:', err);
       }
          console.log('Upload successful!  Server responded with:', body);
       });
     }
     catch (e) {
          console.log(e);
     }

}

我也尝试了不同的实现方式:

  createTask(){
    var token = "0/1234abcd5678efgh9102ijk"
    var bearerToken = "Bearer " + token

     var options = {
       "method" : "POST",
       "headers" : {"Authorization": bearerToken},
     }
     try {
       request.post("https://app.asana.com/api/1.0/tasks?workspace=1234567&projects=765534432&parent=null&name=taskName&assignee=me", options, function optionalCallback(err, httpResponse, body) {
       if (err) {
          return console.error('upload failed:', err);
       }
          console.log('Upload successful!  Server responded with:', body);
       });
     }
     catch (e) {
          console.log(e);
     }

}

根据请求模块提供的示例,您的选项对象似乎使用 payload 作为键,但它应该是 body.