在 Google Apps 脚本 (Google Drive API / REST V2) 中调用 Drive.Files.get(fileId,{alt: 'media'}) 时出错



当我尝试使用 Drive.Files.get(fileId,{alt: 'media'}( 从 Google 应用脚本(我使用的是 Google Drive API/REST V2(获取存储在 Google 驱动器中的文件内容(mimetype 是"text/csv"(时,我遇到了错误。

奇怪的是,我的文件内容似乎在错误消息中返回,如下所示:

{
   "message":"Response Code: 200. Message: idturltthumbnail_urlttitletpublishedAttdescriptiontchanelIdtcategoryIdttagstdurationtdislikeCounttviewCounttlikeCounttcommentCountrn....",
   "name":"HttpResponseException",
   "fileName":"Code",
   "lineNumber":142,
   "stack":"tat Code:142 (updateChannelVideos)n",
   "statusCode":200
}

你知道我如何在不使用UrlFetchApp等服务的情况下从服务器端获取文件的内容吗?

Google Apps Script似乎无法处理来自var myFile = Drive.Files.get(fileID,{alt: 'media'});的响应,如果还没有,您可能希望将其作为错误提交。

编辑:在此处更新了相关问题票证。

您可能会发现,您可以使用DriveApp,而不是使用Drive高级服务。根据代码的其余部分,混合DriveDriveApp调用时不需要其他作用域。

例如,您可以使用:

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

将 mimetype/getBlob 部分添加到您的用例中

最新更新