Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling Drive.Files.get(fileId,{alt: 'media'}) in Google apps script (Google Drive API / REST V2)

I got an error when I try to get the content of a file (mimetype is "text/csv") stored in my Google drive using Drive.Files.get(fileId,{alt: 'media'}) from a Google apps script (I'm using Google Drive API / REST V2).

The strange thing is that the content of my file seems to be returned inside the message of the error, as follow:

{
   "message":"Response Code: 200. Message: id\turl\tthumbnail_url\ttitle\tpublishedAt\tdescription\tchanelId\tcategoryId\ttags\tduration\tdislikeCount\tviewCount\tlikeCount\tcommentCount\r\n....",
   "name":"HttpResponseException",
   "fileName":"Code",
   "lineNumber":142,
   "stack":"\tat Code:142 (updateChannelVideos)\n",
   "statusCode":200
}

Do you know how I can get the content of my file, from the server side, without using service like UrlFetchApp?

like image 264
Lo Bellin Avatar asked Sep 17 '25 06:09

Lo Bellin


2 Answers

It appears that Google Apps Script can't handle a response from var myFile = Drive.Files.get(fileID,{alt: 'media'}); and if not already you might want to file as a bug.

Edit: updated related issue ticket here.

Instead of using the Drive Advanced Service you might find it easier to use DriveApp. Depending on the rest of your code no additional scopes would be required when mixing Drive and DriveApp calls.

For example you could use:

var myFile = DriveApp.getFileById(fileID).getAs('text/plain').getDataAsString(); 
like image 77
mhawksey Avatar answered Sep 19 '25 07:09

mhawksey


I bypassed this issue using:

var fetchstring='https://www.googleapis.com/drive/v2/files/'+FILE_ID
fetchstring=fetchstring+'?mimeType=text/csv&alt=media';
var file_to_upload = UrlFetchApp.fetch(fetchstring, {
    method: "GET",
    headers: {
    Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
    }}).getBlob(); 

Adapat the mimetype / getBlob parts to your use case

like image 45
Lo Bellin Avatar answered Sep 19 '25 06:09

Lo Bellin