Creating an Asana Task using a POST http request

烈酒焚心 提交于 2019-12-12 03:31:39

问题


I'm trying to use the asana-api to create a Task using a POST http request but I keep getting a 400 bad request as a response.

I managed to get data from the Asana-api using ( a GET request ), but I'm having trouble sending data to Asana with ( a POST request )

I'm using the 'request' module to do the api call

here's the error message :

`{"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"}
 ]}`

Here's my code:

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);
     }

}

I also tried a different implementation :

  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);
     }

}

回答1:


Based on the examples provided by the request module, it appears that your options object uses payload as a key, but it should be body.



来源:https://stackoverflow.com/questions/43060610/creating-an-asana-task-using-a-post-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!