2014-01-05 1 views
0

Я хочу, чтобы скопировать вложения из проекта пыльников проекта уже есть вложения на Basecamp здесь код я используюGoogle Apps Script Basecamp создать вложения из anothr проекта

мою проблему в создании приложения я могу загрузить attachemets но я не может создать приложение с правильным содержанием файла созданного файла с неправильным содержанием файла, вернуть неверный файл в то время как я пытаюсь õpe это

Я использовал этот 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); 
     } 

ответ

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