Uploading attachment from blob plus parent parameter

I’m attempting to upload an attachment - trying both as a blob or from a URL.

Big caveat: Docs indicate you cannot add an attachment from Google Drive. Is this true whether you read the file as a blob or get the URL? If so, that greatly hinders things.

Also, the parent parameter on an attachment. Should that be the task gid?

One error I’m receiving is:
{“errors”:[{“message”:“You should specify one of parent”

Note: in code below, I either pass the name and URL of the image or a blob. This code indicates the former.

function asanaFileUpload(fileName, fileUrl, taskId, accessToken) {

  const attachURL = "https://app.asana.com/api/1.0/attachments";

  let attachment = {
    "data": {
      //file" : fileBlob,
      "name": fileName,
      "parent": taskId,
      "resource_subtype": "external",
      "url": fileUrl
    }
  }

  let options = {
    "method": "POST",
    "headers": {
      "Accept": "application/json",
      "Content-Type": "image/jpeg", //"multipart/form-data", //
      "Authorization": "Bearer " + accessToken
    },
    "payload": attachment //JSON.stringify({ data: attachment }
  };

  try {

    var result = UrlFetchApp.fetch(attachURL, options);
    var attachJSON = result.getContentText();
  } catch (e) {
    Logger.log(e);
    return null;
  } finally {

    return JSON.parse(attachJSON).data;
  }

}

Add: Searching for “You should specify one of parent” returns no results. :cry:

Try this url instead.

const attachURL  = `https://app.asana.com/api/1.0/tasks/${taskId}/attachments`

I saw that URL in the documentation somewhere and could not find it again. Do you have a link for that documentation?

That worked to create a linked attachment using the code below. How can I get it to upload the file to Asana. The image location, due to user/data sensitivity, cannot be unrestricted. The task is private to the person it is assigned to and the customer/department wants to image uploaded into Asana.

This worked - again - to create a linked attachments.

function asanaFileUpload(fileName, fileUrl, fileBlob, taskId, accessToken) {

  let attachURL = `https://app.asana.com/api/1.0/tasks/${taskId}/attachments`; 

  let attachment = {
      "name": fileName,
      "resource_subtype": "external",
      "url": fileUrl
    }

  let options = {
    "method": "POST",
    "headers": {
      "Accept": "application/json",
      "Content-Type": "image/jpeg",
      "Authorization": "Bearer " + accessToken      
    },
    "payload": JSON.stringify({ data: attachment })
  };

  try {

    var result = UrlFetchApp.fetch(attachURL, options);
    var attachJSON = result.getContentText();
  } catch (e) {
    Logger.log(e);
    return null;
  } finally {

    return JSON.parse(attachJSON).data;
  }

}

This is what worked. Image uploads correctly and is visible in the task.

Note: This is Google Apps Script

function testFileUpload() {
  const taskId = "[ENTER YOUR TASK ID]";
  const accessToken = "#/########";  //your access token
  const file = DriveApp.getFileById([ENTER AN IMAGE (JPG) GOOGLE DRIVE ID]);
  const fileName = file.getName();  //unneeded
  const fileURL = file.getUrl();    //unneeded
  const fileBlob = file.getBlob();
  const asanaResponse = asanaFileUpload(fileBlob, taskId, accessToken);
  Logger.log(JSON.stringify(asanaResponse,null,4));

}


function asanaFileUpload(fileBlob, taskId, accessToken) {

  let attachURL = `https://app.asana.com/api/1.0/tasks/${taskId}/attachments`; //"https://app.asana.com/api/1.0/attachments";

  let formData = {
       'file' : fileBlob
    }

  let options = {
    'method': 'POST',
    headers: {
      "Authorization": "Bearer " + accessToken      
    },
    'payload': formData
  };

  try {

    var result = UrlFetchApp.fetch(attachURL, options);
    var attachJSON = result.getContentText();
  } catch (e) {
    Logger.log(e);
    return null;
  } finally {

    return JSON.parse(attachJSON).data;
  }

}
2 Likes

Nice that you got it working! Haha sorry i cant find the url in the documentation. I wonder if i found it on the old documentation. Thats the url i use for uploads. :smile:

1 Like