Download a file from DropBox to Google Drive

204 views
Skip to first unread message

Petr Sedlacek

unread,
Nov 8, 2020, 3:31:19 PM11/8/20
to Google Apps Script Community

Hello,

could you please help me to understand how the DropBox API call should be done to get a file from Dropbox to Google Drive?

I've already authenticate it using OAuth2 but I can't set up the API request.

function downloadFileFromDropBox()
{
 
var confirmAuthorization=getBoxService_().getAccessToken();
 
//https://ent.box.com/s/78s1vk70ecm3gzx9zpl85fudc0pjy57g
 
var options={
     
'method' : 'post',
     
'Authorization': 'Bearer ' + confirmAuthorization,
     
'Dropbox-API-Arg': '{"path": "/Scrum_model_feature.xlsx"}',
     
'muteHttpExceptions': 'true'
   
}
 
var response = UrlFetchApp.fetch('https://content.dropboxapi.com/2/files/download', {headers:options}
                                   
);
 
var result=DriveApp.createFile(response);
  result
.setName("GTMList.xlsx");
  result
.setStarred(true);
}


Exception: Request failed for https://content.dropboxapi.com returned code 401. Truncated server response: {"error_summary": "invalid_access_token/", "error": {".tag": "invalid_access_token"}} (use muteHttpExceptions option to examine full response)

I have another function which lists all the folders on DropBox. It works well.

function getFoldersList() {


 
var confirmAuthorization=getBoxService_().getAccessToken();
 
var response = UrlFetchApp.fetch('https://api.box.com/2.0/folders/0/items?fields=name,type', {
    headers
: {
     
'Authorization': 'Bearer ' + confirmAuthorization
   
}
 
});


 
var result = JSON.parse(response.getContentText());
 
var items = result.entries;


 
var folders = [];


 
for (var i=0; i<items.length; i++) {
   
if (items[i].type === "folder") {
      folders
.push({name: items[i].name, id: items[i].id});
   
}
 
}


 
Logger.log(folders);
}




Thank you.

Petr Sedlacek

unread,
Nov 9, 2020, 3:30:28 PM11/9/20
to Google Apps Script Community
Please disregard my question. The biggest challenge was surprisingly the file id. I feel embarrassed.
The file id is not part of a sharing link. The file id consists only from numbers and it is part of the url when opening file on the box.
Here is the working function how to download a file from Box to Google Drive using google apps script.

function fileFromDropBox2Drive()
{
 
//https://broadcom.ent.box.com/file/739530614572?s=0ptjncbno8a7f3raj4spqp6g9bcf7ngs
 
 
var gtmFileId="739530614572"; //GTM created by me
 
var apiHtml="https://api.box.com/2.0/files/"+gtmFileId+"/content/";
 
var confirmAccess=getBoxService_().getAccessToken();
 
 
var options={
   
'Authorization': 'Bearer ' + confirmAccess,
   
'Content-Type':'application/json'
 
}
 
var response=UrlFetchApp.fetch(apiHtml, {headers:options});
 
var gtmFile=DriveApp.createFile(response);
  gtmFile
.setName("fileFromBox.xlsx");
  gtmFile
.setStarred(true);
 
return gtmFile.getId();
 
}

Alan Wells

unread,
Nov 9, 2020, 7:23:49 PM11/9/20
to Google Apps Script Community
Good information to know.
Reply all
Reply to author
Forward
0 new messages