当我尝试使用来自Google应用脚本的Drive.Files.get(fileId,{alt:'media'})获取存储在我的Google云端硬盘中的文件内容(mimetype为“text / csv”)时出错(我正在使用Google Drive API / REST V2)。
奇怪的是,我的文件内容似乎在错误消息中返回,如下所示:
{
"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
}
您是否知道如何在不使用UrlFetchApp等服务的情况下从服务器端获取文件内容?
似乎Google Apps脚本无法处理来自var myFile = Drive.Files.get(fileID,{alt: 'media'});
的响应,如果还没有,您可能希望将其归档为错误。编辑:related issue ticket here。
您可能会发现使用Drive
更容易,而不是使用DriveApp
高级服务。根据代码的其余部分,在混合Drive
和DriveApp
调用时,不需要额外的范围。
例如,您可以使用:
var myFile = DriveApp.getFileById(fileID).getAs('text/plain').getDataAsString();
我使用以下方法绕过了这个问题:
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();
将mime type / getBlob部分调整为您的用例