【问题标题】:Google apps script Basecamp create attachment from anothr projectGoogle 应用脚本 Basecamp 从另一个项目创建附件
【发布时间】:2014-01-05 22:05:42
【问题描述】:

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

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

我用过这个documentation

谢谢

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

【问题讨论】:

    标签: api google-apps-script basecamp


    【解决方案1】:
    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);
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2016-06-01
      • 2021-05-07
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多