2
votes

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?

2
Can you post the code that you are using to fetch the file from Drive. - Amit Agarwal
It is as simple as: var myFile = Drive.Files.get(fileID,{alt: 'media'}); I'm using the Drive advanced service provided by GAS (see: https://developers.google.com/apps-script/advanced/drive) - Lo Bellin
I'm providing a little more background to this: at first I was trying to append new lines to the .CSV hosted on my drive. It seems that cannot be done without getting the content of .CSV first, so here I am. If you know a way to do this using the advanced drive service of GAS I'll be glad to learn! - Lo Bellin
try going though the sheets api if you want to edit the file. I dont think that app scripts is going to let you download a file. all your going to get with files.get is the file metadata not its contents - DaImTo
From your question and comments, I found that you cannot use UrlFetchApp and https://www.googleapis.com/auth/drive%22 of a scope. It seems that in your situation, there are other scopes that you can use and cannot use. And also if there are such limitations for the methods and APIs, can I ask you about them? I think that providing such materials will help users think of your solutions or workarounds. Can you provide such information? - Tanaike

2 Answers

2
votes

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

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