Lo Bellin
Lo Bellin

Reputation: 502

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?

Upvotes: 4

Views: 1786

Answers (2)

mhawksey
mhawksey

Reputation: 2053

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

Upvotes: 2

Lo Bellin
Lo Bellin

Reputation: 502

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

Upvotes: 1

Related Questions