0

我想将附件从项目复制到另一个项目已经在 basecamp 上有附件这是我正在使用的代码

我在创建附件时遇到的问题我可以上传附件,但我无法创建具有正确文件内容的附件创建的文件包含错误的内容文件,当我尝试打开它时返回无效文件

我使用了这个文档

谢谢

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

1 回答 1

0
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);
} 
于 2014-01-05T14:05:42.077 回答