Question

I want to copy attachments from project to anther project already has attachments on basecamp here is the code I'm using

my problem in creating attachments I can upload attachemets but I can't create attachment with right file content the created file is with wrong content file which return invalid file while i try to ope it

I used this documentation

Thanks

 function copyAttachements()
    {
     sourceProjectID=4683117;
     destinationProjectID=4683125;
     var url=getProjectURL(sourceProjectID);

     var unamepass=username+":"+password ;
     var digest = Utilities.base64Encode(unamepass);
     var digestfull = "Basic "+digest;

     var header={Authorization: digestfull};
     var option={ method : "get",muteHttpExceptions:true,headers : header,contentType :      "application/json"};
     var response=UrlFetchApp.fetch(url,option);

     if(String(response.getResponseCode())=="200")
     {
     var name,content;
     url=url.replace(".json","/attachments.json");
     var header={Authorization: digestfull};
     var option={ method : "get",muteHttpExceptions:true,headers : header,contentType : "application/json"};
     var response=UrlFetchApp.fetch(url,option);

     var jsonData=Utilities.jsonParse(response.getContentText());
     for(var j in jsonData)
     {
       var option={ method : "get",muteHttpExceptions:true,headers : header,contentType : "application/json"};
       var response=UrlFetchApp.fetch(url,option);

       content=UrlFetchApp.fetch(url,option).getContent();

       var url = "https://basecamp.com/"+BasecampID+"/api/v1/attachments.json";//getProjectURL(destinationProjectID);

       var data={"content": content,"Content-Length":jsonData[j].byte_size,"Content-Type":jsonData[j].content_type};
       var header={Authorization: digestfull};
       var payload = JSON.stringify(data); 

       var option={ method : "post",muteHttpExceptions:true,headers : header,payload:payload,contentType : "application/json"};
       var response=UrlFetchApp.fetch(url,option);  


       var url =getProjectURL(destinationProjectID);
       url=url.replace(".json","/uploads.json");
       var header={Authorization: digestfull};

       var data={"content": jsonData[j].content,
            "attachments": [{"token": Utilities.jsonParse(response.getContentText()).token, "name": jsonData[j].name}]};
       var payload = JSON.stringify(data); 
       var option={ method : "post",muteHttpExceptions:true,headers : header,payload:payload,contentType : "application/json"};
       var response=UrlFetchApp.fetch(url,option);  
         }
       }
       else
         Browser.msgBox("Error",response);
      } 
Was it helpful?

Solution

function copyAttachements()
{
  sourceProjectID=4683117;
  destinationProjectID=4683125;
  var url=getProjectURL(sourceProjectID);

  var unamepass=username+":"+password ;
  var digest = Utilities.base64Encode(unamepass);
  var digestfull = "Basic "+digest;

  var header={Authorization: digestfull};

  var option={ method : "get",muteHttpExceptions:true,headers : header,contentType : "application/json"};
  var response=UrlFetchApp.fetch(url,option);

  if(String(response.getResponseCode())=="200")
  {
    var name,content;

    url=url.replace(".json","/attachments.json");

    var header={Authorization: digestfull};
    var option={ method : "get",muteHttpExceptions:true,headers : header,contentType : "application/json"};
    var response=UrlFetchApp.fetch(url,option);

    var jsonData=Utilities.jsonParse(response.getContentText()).reverse();
    for(var j in jsonData)
    {
      url=jsonData[j].url;    
      var option={ method : "get",muteHttpExceptions:true,headers : header,contentType : "application/json"};

 /**********************Changes Done here*********************************/
      content=UrlFetchApp.fetch(url,option).getContent();
      var url ="https://basecamp.com/"+BasecampID+"/api/v1/attachments.json";
      var header={Authorization: digestfull};
      var option={ method : "post",muteHttpExceptions:true,headers : header,payload:content,contentType : jsonData[j].content_type};
/***********************************************************************/
      var response=UrlFetchApp.fetch(url,option);  
      var url =getProjectURL(destinationProjectID);
      url=url.replace(".json","/uploads.json");
      var header={Authorization: digestfull};
      var token=Utilities.jsonParse(response.getContentText()).token
      var data={"attachments": [{"token":token, "name": jsonData[j].name}]};
      var payload = JSON.stringify(data); 
      var option={ method : "post",muteHttpExceptions:true,headers : header,payload:payload,contentType : "application/json"};
      var response=UrlFetchApp.fetch(url,option);  
    }
  }
  else
    Browser.msgBox("Error",response);
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top